Python Formal and Actual Arguments

Python Formal and Actual Arguments

We glanced at the Python Functions and Python Pass by Object Reference for functions in our previous two chapters. Here in this chapter, we will discuss the Python Formal and Actual Arguments.


Find the link to all the Python Chapters here: Learn Python


Python Formal and Actual Arguments

We know that a function may contain a few parameters or no parameters at all based on the requirements. The parameters that receive values from outside the function are called formal arguments. And the values that are present outside the function that we pass to the function are called actual arguments. In the example program below, formal arguments are ‘p’ and ‘q’, and the actual arguments are ‘m’ and ‘n’

#python program to display the formal and actual arguments
def addition(p,q): # p, q are formal arguments
    r=p+q
    print("The addition result is: ",r)

#let us call the function
m,n=50,25
addition(m,n) # m, n are actual arguments

Output

The addition result is:  75

There are five kinds of actual arguments that we use while calling a function:

  1. Positional Arguments
  2. Keyword Arguments
  3. Default Arguments
  4. Variable-length arguments
  5. Keyword Variable Length Arguments

Positional Arguments

When you pass arguments to a function in a positional manner, we call them Positional Arguments. You must ensure that the number of positions in the function and the number of arguments you pass should match. 

For example, let us consider a function letusjoin(string1, string2) that has two arguments – string1 & string2. Since the function expects two strings, you should make sure that you pass exactly two strings from outside the function. Otherwise, your code will land in an error.

#python program to show positional arguments
#python program to show positional arguments
def letusjoin(string1, string2):
    string3=string1+string2
    print("The result is: ",string3)
#let us call the function now
letusjoin("Auto","Rickshaw") #positional arguments

Output

The result is:  AutoRickshaw

Keyword Arguments

If the parameters are identified by their names in arguments, then they are called Keyword Arguments. For example, let us consider a function that shows us the product name and the cost of a gadget as, def gadget(product, cost). And while calling this function, you can pass two values by assigning which value is for which parameter like gadget(product=’iPhone’,cost=1000).

In this case, we mention the keyword, product and its value, followed by the following keyword, cost and its value. As you can see, these keywords are nothing but the parameter names that receive the values. If you follow this way, you’d also get the liberty to change the order of the parameters like gadget(cost=1000, product=’iPhone’), and it will not create any errors for you, and the program will function as intended.

#python program to show keyword arguments
def gadget(product,cost):
    print("The product name is: ",product)
    print("and its cost is: ",cost)

#calling the gadget function
gadget(product='GooglePixel',cost=900) #keyword arguments
gadget(cost=1000,product='iPhone') #keyword arguments

Output

The product name is:  GooglePixel
and its cost is:  900
The product name is:  iPhone
and its cost is:  1000

Default Arguments

Python allows you to set default values for parameters when you declare a function. For example, let us consider the same function from the above example once again. If you want to set a default value for the cost in this function, you can set it as gadget(product,cost=1000)

It means that if you don’t pass the value of cost while calling the function and pass only the product’s value, the function will make use of the default cost value, which is 1000 and displays the same in the output. However, if you pass both product and cost values, the default cost value also gets updated along with the product value with the new values that you pass to the function.

#python program to show default arguments
def gadget(product,cost=1000):
    print("The product name is: ",product)
    print("and its cost is: ",cost)

#calling the gadget function
gadget(product='GooglePixel',cost=900) #passing two arguments
gadget(product="iPhone")
#the default value of cost will be used now
#since we have not passed the cost while passing the iPhone value

Output

The product name is:  GooglePixel
and its cost is:  900
The product name is:  iPhone
and its cost is:  1000

Variable Length Arguments

We have seen the examples in which we passed the values based on the number of values a function can receive. For instance, in the case of the addition(p,q) function, we passed only two values since we knew that it could accept only two values.

What if you are a programmer, who is new to this program created by someone else, and you don’t know how many values a function in this program can accept? And you accidentally sent three arguments like addition(10,30,40). These accidental situations will result in errors and affect the user experience of your programs.

However, Python has a solution for you in these cases. Using the variable-length argument, you can create a function that can receive any number of arguments.

Its format is def addition(farg, *args). The ‘*’ symbol is used to denote that it is a variable-length argument. And the ‘farg’ represents the formal argument. All the values that are passed to the variable-length argument get stored in a tuple, and you can use a for loop to display the values as mentioned in the program below.

#python program to demonstrate the variable-length arguments
def addition(farg,*args): #args can accept any number of values
    print("The formal argument is: ",farg)

    count=0
    for num in args:
        count+=num
    print("The total count is: ",(farg+count))

#let us call the addition function and pass values
print("When you pass 30 and 40")
addition(30,40)
print("When you pass 20,30,40,50 and 60")
addition(20,30,40,50,60)

Output

When you pass 30 and 40
The formal argument is:  30
The total count is:  70
When you pass 20,30,40,50 and 60
The formal argument is:  20
The total count is:  200

Keyword Variable Length Arguments

If a function can accept any number of values in the format of keys and values in an argument, then this argument is called a keyword variable length argument. To declare the argument as a keyword variable length argument, you should declare it with ‘**’ prior to the argument.

For example, def showoutput(farg, **kwargs). Here, farg is called the formal argument and ‘**kwargs’ is called the keyword variable argument.

#python program to demonstrate the key variable-length arguments
def showoutput(farg,**kwargs): #kwargs can accept any number of values
    print("The formal argument is: ",farg)

    for a,b in kwargs.items(): #items() can be used to pair of items
        print('key={},value={}'.format(a,b))

#let us call the function
#and try passing several keyword arguments
print("Passing 1 formal argument and 4 keyword arguments")
showoutput(7,nm=10,currency="Dollar")
print("Passing 1 formal argument and 6 keyword arguments")
showoutput(7,nm=30,currency='Rupee',country='India')

Output

Passing 1 formal argument and 4 keyword arguments
The formal argument is:  7
key=nm,value=10
key=currency,value=Dollar
Passing 1 formal argument and 6 keyword arguments
The formal argument is:  7
key=nm,value=30
key=currency,value=Rupee
key=country,value=India

We hope you have understood the Python Formal and Actual Arguments through this post. 🙂 !!

Source

Scroll to Top