Function parameters are variables that we can pass to the function when calling it, for it to use. Parameters make functions more flexible and reusable.
The basic syntax for defining a function with parameters in Python is as follows:
def function_name(parameter1, parameter2, ...):
# code block of the function that uses the parameters
parameter1,parameter2,… are the function parameters
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, it prints a greeting “Hello” followed by the name passed as an argument.
Now, if I call the function with the parameter Luis, this causes the function to execute and display the message Hola Luis as output.
If you want to learn more, check out the Introduction to Programming Course
Types of Parameters in Python
Positional Parameters
Positional parameters are the most common parameters. 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 add(a, b):
result = a + b
return result
# Calling the function with positional parameters
result_addition = add(5, 3)
print(result_addition) # Output: 8
Parameters with Default Values
Parameters with default values are parameters that have a value assigned by default in the function definition.
def greet(name="World"):
print("Hello,", name)
# Calling the function without providing a value for the parameter
greet() # Output: Hello, World
# Calling the function with a value for the parameter
greet("Luis") # Output: Hello, Luis
If no value is provided for these parameters when calling the function, the default value will be used.
Named Parameters
Named parameters are parameters that are passed to the function using their name (instead of their position).
This allows changing the order of the parameters or even omitting some of them.
def greet(name, greeting="Hello"):
print(greeting + ",", name)
# Calling the function 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
*args: Used to pass a list of positional arguments
def add_numbers(*args):
result = sum(args)
return result
# Calling the function with a variable number of arguments
result_addition = add_numbers(1, 2, 3, 4, 5)
print(result_addition) # Output: 15
**kwargs
**kwargs: Used to pass a dictionary of keyword arguments
def print_info(**kwargs):
for key, value in kwargs.items():
print(key + ":", value)
# Calling the function with named arguments
print_info(name="Luis", age=30, city="Madrid")
# Output:
# name: Luis
# age: 30
# city: Madrid
Pass by Value or by Reference
In Python, passing arguments to functions is always done by value (meaning the function always receives a copy of the argument).
But the behavior will be different depending on whether we pass a basic type or a reference.
- For a basic type, the called function cannot modify the argument it receives
- For a reference type, it can modify the argument it receives.
Let’s see it with an example. First, let’s see the behavior when we pass a basic type. For example, we pass 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 what happens with 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(lst):
lst.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]
