Functions
Learn how to write functions (including anonymous ones) in Python.
StartKey Concepts
Review core concepts you need to learn to master this subject
Functions
Function Keyword Arguments
Function Parameters
Function Arguments
Returning Value from Function
Multiple Parameters
Calling Functions
Recursion in Python
Functions
Functions
# Define a function my_function() with parameter x
def my_function(x):
return x + 1
# Invoke the function
print(my_function(2)) # Output: 3
print(my_function(3 + 5)) # Output: 9
Some tasks need to be performed multiple times within a program. Rather than rewrite the same code in multiple places, a function may be defined using the def
keyword. Function definitions may include parameters, providing data input to the function.
Functions may return a value using the return
keyword followed by the value to return.