Python Function
Python Function
Defining Functions
Function definition begins with “def.” Function name and its arguments.
def get_final_answer(filename):
“““Documentation String”””
line1
line2 Colon.
return total_counter
Run example »
Return Values
Example
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Output
15
25
45
Functions are first-class objects
Functions can be used as any other
datatype, eg:
• Arguments to function
• Return values of functions
• Assigned to variables
• Parts of tuples, lists, etc
>>> def square(x): return x*x
>>> def applier(q, x): return q(x)
>>> applier(square, 7)
49
Lambda Notation