How to pass a parameter to a function in Python
Overview
A function is a container for a few lines of code that perform a simple task. Defining a function in Python means that we are creating a function.
Functions in Python are created and denoted with the def keyword followed by a function name. For example, greet_customer followed by a set of parentheses (). The parameters of the function are stored in the parentheses.
Parameters
A parameter is a variable listed inside the parentheses in the function definition. For example greet_customer(name) is a function that has (name) as its parameter.
How to pass a parameter to a function
The purpose of function parameters in Python is to allow a programmer using the function to define variables dynamically within the function.
It’s important to note that after passing a parameter to a function, an argument should be passed to the parameter of the function whenever you call the key function in your program.
Note: An argument is a value that is supplied to the parameter whenever the function is called.
Now, let’s pass a parameter to a function that will help us store the name of our customers by returning the argument (real name) we pass to that function.
Code
# defining a funtion and passing the name parameter to itdef greet_customer(name):print(f'Hi {name} welcome aboard to the home of delicacies!')# calling the function and passing Theophilus as the argumentgreet_customer('Theophilus')# calling the function and passing Chidalu as the argumentgreet_customer('Chidalu')
Explanation
-
In the code above, we use the
defkeyword to show that we are defining a function. -
The name of our function is
greet_customer(name).(name)is the parameter of the function. -
The double line breaks must be included after defining a function.
-
(Theophilus)and(Chidalu)are the arguments passed to thegreet_customer(name)function whenever it is called. -
greet_custumer(Theophilus)andgreet_customer('Chidalu)are used to called the key function,greet_customer(name). Without calling the key function, the program will not execute the defined function.
You can also add multiple parameters to your function.
Example
From the previously defined function, greet_customer(name), instead of containing just one parameter, let’s create a new function that will contain multiple parameters, (first_name), (middle_name), and (last_name)
Code
# defining the function and passing multiple parameters to itdef greet_customer(first_name, middle_name, last_name):print(f'Hi {first_name} {middle_name} {last_name} welcome aboard')# calling the function and passing arguments to itgreet_customer('Theophilus', 'Chidalu', 'Onyejiaku')
Explanation
The most important thing to note about multiple parameters is that whenever you create them, you need to separate each of them with a comma. The same is done when passing their arguments.
Any parameter given to a function must have its corresponding positional argument.
Example
Let’s define a function with two parameters, first_name and last_name. Now let’s see what happens when we do not give corresponding positional arguments to the parameters of the function.
Code
# defining a function and passing the parametersdef greet_customer(first_name, last_name):print(f'Welcome abord {first_name} {last_name} to the home of delicacies')# Now lets pass a wrong corresponding positional argumentgreet_customer('Theophilus')
Notice that the code above gives an error message that reads as follows.
TypeError: greet_customer() missing 1 required positional argument: 'last_name'
What this means is that while we defined the greet_customer(first_name, last_name) function with two parameters, we only gave a positional argument to the first parameter, first_name, and ignored the other, last_name. The code below gives the solution for the error message from the previous code.
# defining a function and passing the parametersdef greet_customer(first_name, last_name):print(f'Welcome abord {first_name} {last_name} to the home of delicacies')# Now lets pass a correct corresponding positional arguments for the two parametersgreet_customer('Theophilus', 'Onyejiaku')
The program above is successful simply because we give the correct corresponding positional arguments for the parameters of the function.