Functions are blocks of code that perform a specific task and can be reused in different parts of a program.
Structuring our code into functions helps us make it more organized, making it more modular, easier to maintain, and simpler to reuse.
If you want to learn more, check out the Introduction to Programming Course
Defining a function
In Python, a function is defined with the keyword def, followed by the function name and parentheses that can contain input parameters. So,
def function_name(parameters):
# Function code block
# May contain multiple statements
return result
- function_name: This is the name we give to our function.
- parameters: These are the parameters our function can receive. They are optional and can be used to pass information to the function.
- return result: The
returnstatement allows us to return a result from the function. It is optional and can return any type of data.
Calling a function
Once we have a function defined, it can be called from anywhere in the program (this is also called invoking the function).
To call a function, we use its name followed by parentheses. If the function requires arguments, we must place them inside the parentheses.
For example, if we have this function:
def greet():
print("Hello Luis")
We can invoke it like this:
greet()
In this case, the greet function is called, which will cause the message “Hello Luis” to be displayed.
Parameters and arguments
Parameters are variables that we can pass to our function when we invoke it. This allows us to create much more flexible and reusable functions.
For example, imagine we have a function that simply adds two numbers. We can make it receive two parameters and perform the addition.
In this case, a and b are parameters of the sum function (we could have chosen any other names for a and b, the names are up to us)
def sum(a, b):
print(a + b) # Output: 8
sum(3, 5) # we call the function
In the example, we called the function passing 3 and 5 as arguments. We could call it with any other numbers, which allows our function to be reusable.
Parameters are the variables defined by the function. In the example a and b
On the other hand, arguments are the specific values we pass to the function in an invocation. In the example 3 and 5
But don’t stress too much about this detail
Return values
Functions in Python can return a value using the keyword return. This value can be assigned to a variable when calling the function.
def giveNumber():
return 8
result = giveNumber()
print(result) # Output: 8
For example, here the function giveNumber() returns 8.
Basic example
If we combine the above, we can already make a very simple function that adds two numbers and returns the result:
def add(a, b):
result = a + b
return result
In this example
- The
addfunction receives two parametersaandb - Adds these values
- Returns the sum result
Now we can call this function and use it in our program:
sum_result = add(5, 3)
print("The result of the addition is:", sum_result) # Output: The result of the addition is: 8
