Function parameters are variables that are used to pass information to the function when it is called.
Parameters allow functions to be more flexible and reusable by allowing them to work with different data on each call.
The basic syntax for defining a function with parameters in Python is as follows:
If you want to learn more
consult the Introduction to Programming Course read more
def function_name(parameter1, parameter2, ...):
# function code block that uses the parameters
In this example, parameter1
is a required parameter and parameter2
has a default value.
Let’s see an example:
def greet(name):
print("Hello", name)
# Calling the function with an argument
greet("Luis") # Output: Hello Luis
Here we define a function called greet
that takes a parameter called name
. Inside the function, a greeting “Hello” followed by the name passed as an argument is printed.
Now, if I call the function with the parameter Luis
, this causes the function to execute and display the message “Hello Luis” as output.
Types of parameters in Python
Positional parameters
Positional parameters are the most common parameters in Python. They are defined in the order they appear in the function’s parameter list and are passed to the function in the same order.
def sum(a, b):
result = a + b
return result
# Function call with positional parameters
sum_result = sum(5, 3)
print(sum_result) # Output: 8
Parameters with default values
Parameters with default values are parameters that have a value assigned by default in the function definition.
If a value is not provided for these parameters when calling the function, the default value will be used.
def greet(name="World"):
print("Hello,", name)
# Function call without providing a value for the parameter
greet() # Output: Hello, World
# Function call with a value for the parameter
greet("Luis") # Output: Hello, Luis
Named parameters
Named parameters are parameters that are passed to the function using their name. This allows changing the order of the parameters or even omitting some of them.
def greet(name, greeting="Hello"):
print(greeting + ",", name)
# Function call with named parameters
greet(name="Ana") # Output: Hello, Ana
greet(greeting="Good morning", name="Pedro") # Output: Good morning, Pedro
Variable-length parameters
Python allows defining functions that accept a variable number of parameters using *args
and **kwargs
.
*args
: It is used to pass a list of positional arguments.
def sum_numbers(*args):
result = sum(args)
return result
# Function call with a variable number of arguments
sum_result = sum_numbers(1, 2, 3, 4, 5)
print(sum_result) # Output: 15
**kwargs
: It is used to pass a dictionary of keyword arguments.
def print_info(**kwargs):
for key, value in kwargs.items():
print(key + ":", value)
# Function call with named arguments
print_info(name="Luis", age=30, city="Madrid")
# Output:
# name: Luis
# age: 30
# city: Madrid
Using *
and **
when calling a Function
When we have a list or dictionary and we want to pass its elements as arguments to a function, we can use *
and **
respectively.
- The
*
operator is used when we want to unpack the elements of a list and pass them as arguments to a function.
def add(a, b, c):
return a + b + c
values = [1, 2, 3]
print(add(*values)) # Result: 6
Here, values
is a list containing [1, 2, 3]
, and when calling add(*values)
, the elements of the list are unpacked and passed as arguments to the add
function, so it is equivalent to calling add(1, 2, 3)
.
- The
**
operator is used similarly, but to unpack the elements of a dictionary and pass them as arguments to a function, where the keys of the dictionary become parameter names and the values of the dictionary are assigned to those parameters.
dictionary = {"a": 1, "b": 2, "c": 3}
print(add(**dictionary)) # Result: 6
Here, dictionary
is a dictionary containing {"a": 1, "b": 2, "c": 3}
, and when calling add(**dictionary)
, the elements of the dictionary are unpacked and passed as arguments to the add
function, where the key "a"
is assigned to the parameter a
, the key "b"
is assigned to b
and the key "c"
is assigned to c
, so it is equivalent to calling add(a=1, b=2, c=3)
.
Pass by value or by reference
In Python, passing arguments to functions is always done by value. This means that the function always receives a copy of the argument. But the behavior will be different if we pass a basic type or a reference.
- If it is a basic type, the invoked function cannot modify the argument it receives
- If it is a reference, it can modify the argument it receives.
This can have implications on how functions handle mutable arguments, such as lists and dictionaries.
Examples
First, let’s see the behavior when we pass a basic type, for example an integer (int
) to the modify_number()
function. Inside the function, we multiply the number by 2. This modification does not affect the original number outside the function.
# Example with a basic type (int)
def modify_number(number):
number = number * 2
original_number = 5
modify_number(original_number)
print("Original number after calling the function:", original_number) # Result: 5
Now let’s see a reference type, for example a list that we pass as a reference to the modify_list()
function. Inside the function, we add the number 4 to the list. The modification made inside the function is reflected in the original list outside the function.
# Example with a reference (list)
def modify_list(list):
list.append(4)
original_list = [1, 2, 3]
modify_list(original_list)
print("Original list after calling the function:", original_list) # Result: [1, 2, 3, 4]