In this article, you will master python function arguments and parameters. In the previous article, you learned about the basic of a function in python.
We used this code to understand how functions work in python.
def sum(value1, value2):
value3 = value1 + value2
return value3
#main code to call the function
total = sum(3,4)
print(total)
#output 7
Now we will dive deep into it to understand everything about arguments and parameters in functions in python programming.
What are Python Function Arguments?
A python function argument is a value you pass in the function when you call it. In the code mentioned above, 3 and 4 are the arguments of the function sum().
total = sum(3, 4)
Arguments are the values passed in the function when you call the function in the main code.
Anytime when you call the function and pass any value to the function, that value will be called as the argument of the function.
total = sum(8, 2)
Now, 8 and 2 are the arguments of the function sum().
What are Python Function Parameters?
A python function parameter is a variable listed inside the parenthesis when you define a function.
In the case of this function, value1 and value2 are the parameters of the function sum().
def sum(value1, value2):
What is the difference between argument and parameter?
The difference between argument and parament is pretty simple.
A parameter is a variable listed inside the function when you define a function.
You can define a parameter once in a program.
def sum(value1, value2):
In this code, you are defining a function sum. You are passing two variables inside the parenthesis. These variables are called the parameters of function sum().
On the other hand, an argument is a value you pass in a function when calling it.
This value may change every time you call the function.
total = sum(5, 4)
In the above code, 5 and 4 are the arguments to the function sum in the first call. The function will add 5 and 4 and return 9 as a result. This result will store in the total variable.
You can change these arguments when you call the function again.
total = sum(8, 7)
The function sum() arguments changed to 8 and 7. The function will return 15. Variable total will store this result.
Python Arguments and Parameters Examples
Now, you have learned the difference between arguments and parameters. It is time to have a look at some easy examples to make the concepts even clear.
#function to print table of any number
def table(number):
for i in range(1, 11):
print(i*number, end=" ")
#driver code to call the function
table(2)
#this code will print the table of 2
#output 2 4 6 8 10 12 14 16 18 20
In the second line of this code, the variable number is the parameter of the function table.
def table(number):
But when you called the function, you passed 2 as input. That 2 is the argument of a function in python.
table(2)
Now, you have learned everything about the python function arguments and parameters. In the next tutorial, you will master the return statement in python programming.
You will learn what a return is and how to use return statements in your programs effectively.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.