Python Functions : Parameters & Arguments
INTRODUCTION
\
As a beginner learning Python, we are always coming across the terms parameters and arguments frequently and many new coders are often confused with the two different terms parameters and arguments, with the consequence that they even use it wrongly many times. This post is to sort out the differences between the two terms, parameters and arguments accordingly to their usage in Python functions.
WHAT ARE PARAMETERS
A parameter in Python is actually a variable created in that function, that works like a placeholder for some data value that the function will be use later. For example, if we consider an example a function add_num which is used to add two numbers, then it will be written the following manner:
def add_num(x,y):
In this above situation, add_num is the name of the function with a task of adding the two number variables x and y. def or define is used to define or descraibe the function by the name of add_num and here inside the parenthesis () of the function, we are writing the variables x and y with a comma in between them and therefore, here, x and y are the parameters of the function add_num()
We can later on replace the parameaters x and y with the actual data or value for executing the calculation as we require.
WHAT DOES THE PARAMETER DO
The parameter indicates not only the type of data or input but also, the number of inputs that the function will accept . So, this particular role of a parameter works like part of a function signature to the function. For example in the above function that was created by the name of add_num() with two parameters x and y, it indicates that the function add_num() will accept two inputs and the inputs will be in the data form of numbers . If the function will accept three numbers, we should write add_num(x,y,z) where z will be the third number to be accepted by the function add_num()
As you can observe, the parameters are added to the function in the beginning stage of the function when it is defined/described or rather declared.
WHAT ARE ARGUMENTS
Arguments are the actual specific real time values provided to the function whenever the function is called. In other words, whenever a function task is executed, we pass
For example, in the above function, let us sese when we add the arguments to the function.
def add_num(x,y):
sum = (x + y)
return sum
add_num(700, 4000)
solution = add_num(700, 4000)
print(solution)
In this case the arguments are clearly, the number or the data values, 700 and 4000 respectively. As we can see here, after defining or declaring the function add_num() with two parameters x and y, we create another variable called sum to store the added values of the numbers x and y and then after that when we want to execute the function, we call the function by it's name. This is analogous to calling a person by name and ask that person to execute some task! Before calling the function, we write return the variable sum, that is , we are asking the computer to return the values of the added numbers and return the stored value, in other words, we are asking the computer to add the variables x and y and return the added value . After that we call the function by its name that is, add_num in this case and provide specific values of the numbers inside the parenthesis, in this case the provided numbers are 700 and 4000 respectively. By doing this we are calling the function to execute the task of adding the specific provided numbers 700 and 4000. As you can see, while we ask the computer to return the sum, it had only x and y values that were instructed to be added together but no specific values provided to the function. So, during the execution stage, we call the function and provide the specific number values inside its parenthesis in order to execute the addition as required for the provided number values.
In the above situation, we call the function by name , add_num and then mention specific data values or number values in place of x and y respectively. So, x and y that were the parameters working like placeholders in the function, now gets replaced with the actual data values in order to execute the final calculation. So, arguments bring on dynamic and flexible flow to the function for its execution of tasks.
WHAT DOES ARGUMENTS DO
Arguments help the function to execute the specified task and hence very important to be passed to a function. Arguments also help the programmer to call the function , since it is vital to pass real specific values to the function, that is, it is very important to pass arguments to the function, in order to call the function. We cannot call function by passing parameters, but, we need to pass arguments to the function in order to call it and also to execute the task inside the function.
Arguments help to execute the function to carry out specific tasks. Consider the code block here :
def add_num(x,y):
sum = (x + y)
return sum
add_num(700, 4000)
solution = add_num(700, 4000)
print(solution)
As mentioned earlier, x and y are the parameters added during the definition of the function and during the execution of the function, we call the function by its name as shown in the code block, we call it by its name add_num and while calling it, we add the specific numbers, 700 and 4000, inside the parenthesis of the function. Here, the actual number values or the parameter values, 700 and 4000 work as arguments for the function. Finally we print out the the outcome after calling the functtion and passing the actual parameter values, or in other words, after passing the arguments to the function, during the execution time. So, Arguments help in executing the function. Here it will execute the adding up of both the actual numbers , 700 and 4000 and the print function will display the outcome of the addition to the user.
CONCLUSION
In the above post we understood the basic differences between parameters and arguments. T o conclude, parameters work like default placeholders in a function, whereas, arguments work like actual specific data values to replace the placeholders.
Parameters are added at the time of declaration of definition of a function right at the beginning of the time when the function is created, but, arguments are added only when the function is called, during the execution stage of the function.
Hence parameters work like default templates or placeholders whereas arguments are actual specific values.
Parameters help to declare a function while defining it , but, Arguments help to call the function and execute the function to produce the required and relevant outcome. Arguments are very flexible and dynamic since it provides real time actual values for the execution of the function.
Comments
Post a Comment