Functions, Scoping and Abstraction
Functions, Scoping and Abstraction
versus
● Formal parameters of the function are the sequence of names within parantheses
following the function name (x, y in the above example)
● The function call (or function invocation) contains arguments (also called the actual
parameters)
● When the function is called, the formal parameters are bound to the actual
parameters (in above eg, the invocation maxVal(4,3) binds x and y to 4 and 3,
respectively)
Data Science Education and Research www.insofe.edu.in 15
Function calls as expressions
● A function call is an expression
○ Value of the expression is the value returned return by the function
○ Example:
■ maxVal(3,4) is an expression with value 4
■ maxVal(3,4)*maxVal(3,2) is an expression with value 12
● Return statements
○ After a return statement is executed
■ the rest of the code inside the function body is not executed, and
■ the point of execution is transferred to the code immediately following
the function invocation
○ Can have more than one return statement in the function body, but at most
one of them is executed
Data Science Education and Research www.insofe.edu.in 16
Quiz
What is the output of the program below?
z = 6
res = 2*maxVal(3+4, z)
print(res)
a = times(3, 4)
print(a)
b = times('hello', 3)
print(b)
Output:
12
hellohellohello
Output: in f(x): x = 4
Back to main body: x = 3
Data Science Education and Research www.insofe.edu.in 21
Warning if no return statement
● Python returns the value None, if no return statement is given
○ Indicates the absence of value
○ To write a procedure (rather than function) f(x,y), just use def f(x,y): and
have no return statement
● Example:
Output: None
Data Science Education and Research www.insofe.edu.in 22
return vs. print
return print
Output:
inside func_a
None
inside func_b
7
inside func_c
inside func_a
None
Output:
x = 4
z = 4
x = 3
y = 2
Output:
Output:
[2, 4]
4
Abc
Abc
<class 'NoneType'>
None
Output:
in g(x): x = 4
4
3
add(1,2)
print(add(2,3))
mult(3,4)
print(mult(4,5))
def f(x):
return x**2
calc = sq(f, 2)
print(calc)
Output:
Ashwin Ganesan
Ashwin Ganesan
Ashwin Ganesan
Ashwin Ganesan
Data Science Education and Research www.insofe.edu.in 40
Keyword arguments
Once a formal parameter is mentioned in the function call, all later arguments must also mention formal
parameters. The function call below gives an error message because “lastName” is mentioned in the
second argument but the third argument doesn’t mention a formal parameter.
Output:
printName("Ashwin", lastName = "Ganesan", False)
^
SyntaxError: positional argument follows keyword argument
Output:
Ashwin Ganesan
Ganesan, Ashwin
Ganesan, Ashwin
Data Science Education and Research www.insofe.edu.in 42
Functions, Scoping and Abstraction
- Specifications
Instructor: Prof. Ashwin Ganesan
International School of Engineering (INSOFE)
def is_even( i ):
"""
Input: i, a positive int
Output: A boolean, equal to True if i is even, and False otherwise
"""
return i%2 == 0
print(is_even(3))
Output:
False