Functions
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Defining Functions
Functions in Python are defined using the def keyword, followed by the function name and a pair of parentheses. The function body is indented below the declaration.
Pythondef my_function():
print("Hello from a function")
my_function() # Calling the my_function
Function Parameters
Information can be passed into functions as parameters.
Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a function with one parameter (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:
Pythondef my_function(fname):
print(fname + " Refsnes")
my_function("Emil") # Return string "Emil Refsnes"
Arbitrary Arguments, *args
If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly:
Pythondef my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
Keyword Arguments
You can also send arguments with the key = value syntax.
This way the order of the arguments does not matter.
Pythondef my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
Arbitrary Keyword Arguments, **kwargs
If you do not know how many keyword arguments that will be passed into your function, add two asterisk: ** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items accordingly:
Pythondef my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Tobias", lname = "Refsnes")
Default Parameter Value
The following example shows how to use a default parameter value.
If we call the function without argument, it uses the default value:
Pythondef my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
The pass Statement
function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error.
Pythondef myfunction():
pass
Lambda Functions
Lambda functions, also known as anonymous functions, are small, one-line functions defined using the lambda keyword.
Pythonmultiply = lambda x, y: x * y
# Calling the lambda function
result = multiply(3, 4)
# Result: 12
Recursion
Recursion is a programming concept where a function calls itself directly or indirectly. It is often used to solve problems that can be broken down into smaller, similar sub-problems.
Pythondef factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Calling the recursive function
result = factorial(5)
# Result: 120