Estimated reading time: 5 minutes
Are you using a function and looking to pass values into it to be processed? Well if this is the case you are trying to pass arguments to that function’s parameters so they will return a value for you.
Parameters are just values that are associated with the function. For each function, you are going to have one or more parameters it is expecting to process.
As a result, there are a number of ways they are created, let’s explore how this can be achieved.
Passing arguments directly into a function parameters
In a lot of instances, the argument values to be processed are going to be passed into the function parameters, which then executes some logic and returns a value.
Typical ways this may happen:
- A password is entered ( the inputted argument), and the function then confirms if it is valid or not.
- You may need to check if a value entered ( the inputted argument) is above or below a certain value. It is passed to a function parameter and then the function does the necessary checks and confirms the result in the output.
The above are two examples, but let’s see this in action.
Below we have a function. The purpose is to take in two arguments passed to it and conduct a calculation on them.
The calculation happens in the variable number 1 or number 2.
Variable number 1 and variable number 2 get their values from parameters a and b respectively.
The line multiplenumber(1,2) is just calling the function and passing the arguments to the parameters a,b which then processes them and the output is printed.
def multiplenumber(a,b):
number1 = a*2
number2 = b*3
print(number1,number2) #===> These values are different to the variables below as they happen as a result of the processing inside the function.
number1=1
number2=2
multiplenumber(number1,number2)
print(number1,number2) #===> These values are outside the function and do not have any relationship to the variables inside the function above, hence are different.
Result:
2 6
1 2
The result of the above piece of logic is called “passing by value“, which essentially means the data is passed from the variables ( number 1 and Number 2) to the function parameters, but when the function is completed processing, it does not impact the original variables outside the function.
This is because the original variables are immutable.
Passing arguments directly into function parameters from a list
In the above example, as outlined, there will be no impact on the variables that pass the arguments into the function, they remain the same.
On the other hand, if the original source of the arguments is a mutable object, it will have its values changed.
Let’s look at a situation where this may occur.
def addnumber(a):
number=['3']
a.append(number)
print(a) #===> This prints the original value of a with the addition of the value 3
number1=[1,2]
addnumber(number1)
print(number1) #===> As number1 is passed to the function, and number1 is allowed to be changed then this will print out the new list value once the function has completed processing.
Result:
[1, 2, ['3']]
[1, 2, ['3']]
So in this instance, as number1 is a list, and this is mutable, its value will change once the function completes its processing.
This can also be called “passing by reference“.
Passing arguments between functions
In another video, we completed called how to pass data between functions discusses how you can pass the data between two functions and print it out.
Effectively in the below the arguments are name, country, and city, and these values are passed to functionb
def functiona():
name = "Joseph"
country = "Mars"
city = "atlantis"
#this captures the three variables above
return name,country,city
functiona()
def functionb():
myname,mycountry,mycity = functiona()
print(myname)
print(mycountry)
print(mycity)
functionb()
Result:
Joseph
Mars
atlantis
Passing arguments using *args as the parameter
In order to understand this, before we look at some code we need to understand what are *args?
*args essentially allows you to pass multiple positional arguments to a function, and this can vary over time.
As a result, you don’t have to assign the positional arguments to more than one variable. The *args take in all the arguments, no matter how long or short they are and then return a value.
def addnumbers(*args):
result = 2
for x in args:
result = result * x
return result
print(addnumbers(1,2,3))
Result:
12
Passing arguments using **kwargs as the parameter
In contrast to the above, this will process a set of values that have a name attached to them.
As can be seen in the previous example the arguments passed had no name attached to them.
Let’s look closely at an example. In this scenario, the arguments are assigned a value of a,b,c.
def addnumbers(**kwargs):
result = 2
for x in kwargs.values():
result = result * x
return result
print(addnumbers(a=1,b=2,c=3))
Result:
12
Here it is clear that the logic takes in each value of a,b,c processes it and then returns the result required.
In essence, both *args and **kwargs give you the same answer, it just depends on other parts of your code and how it is processing your data.
For example, in the **kwargs it could be that the values of a,b,c could be populated by other variables within the program.
So in that scenario, the variable that has been passed on can change and not be static.
So in summary:
a. Arguments can be passed multiple ways to functions, the parameters then just process them.
b. Parameters also don’t have to be assigned to a variable, they can be read through *args.
c. On the other hand you can have multiple arguments read in at once by using the **kwargs.