Thursday, February 3, 2022

Functional arguments

Python programmers often use parameters and arguments interchangeably as both are in a way quite similar but as a Python developer, you must understand the difference between the two. Parameters are declared in the function, and arguments are the values passed to a function when it is called. There are five types of functional arguments in Python.

Positional arguments

In positional arguments, that is, arguments are assigned to the parameters in the order in which they are passed or their position.

def sum_prod(num1,num2):

num_sum = num1 + num2

num_prod = num1 * num2

return num_sum,num_prod

x = int(input('Enter the first number :'))

y = int(input('Enter the second number :'))

print(sum_prod(x,y))

Output:

Enter the first number :10

Enter the second number :20

(30, 200)

>>>

The positional argument looks at the position of the parameter where it will be assigned. So, the value of x gets mapped to num1, and value of y gets mapped to num2 and these values are passed on to the function block code.

If you call a function with different number of parameters, an error will be generated.

Default arguments

You have the option of specifying default value of a parameter in the function definition. The positional argument for which a default value is defined, becomes optional and therefore known as default argument.

def sum_prod(num1,num2 =0):

num_sum = num1 + num2

num_prod = num1 * num2

return num_sum,num_prod

print(sum_prod(2,5))

print(sum_prod(2))

Output:

(7, 10)

(2, 0)

>>>

The function shown above can be called with one or two arguments. If you omit the second argument, the function definition will pass on its default value, which is 0.

In Python, a non-default argument cannot follow a default argument.

def sum_func(num1,num2 =0,num3):

return num1+num2+num3

The preceding function will throw an error because num3 which is a nondefault argument follows num2, which is a default argument. So, if you type sum_func(10,20), the interpreter will not understand whether to assign 20 to num2 or continue with the default value. The complexity will increase as the number of default arguments increase. In this scenario you will receive a Syntax Error: "non default argument follow default argument". The correct way of using default arguments is shown in the following code:

def sum_func(num1,num2 =30,num3=40):

return num1 + num2 + num3

print(sum_func(10))

print(sum_func(10,20))

print(sum_func(10,20,30))

Output:

80

70

60

Keyword arguments

Keyword arguments allow you to ignore the order in which the parameters are entered in a function or even skip them when calling a function.. The function with keyword arguments are defined the same way as the function with positional arguments, but the difference is in the way they are called. Have a look at the following code:

def sum_func(num1,num2 =30,num3=40):

print("num1 = ", num1)

print("num2 = ", num2)

print("num3 = ", num3)

return num1 + num2 + num3

print(sum_func(num3 = 10, num1 =20))

Output:

num1 = 20

num2 = 30

num3 = 10

60

As you can see, the arguments are not passed in the desired order, but while passing the arguments, it is specified which argument belongs to which parameter. Since the default value of num2 is zero, even if it is skipped, it does not matter. We wanted to use the default value of num2 therefore only the value of num1 and num3 were specified. If that is not done, the output will be incorrect. As you can see in the following code, the value 20 is assigned to num2 and default value of num3 is taken as a result of which the result is completely different.

def sum_func(num1,num2 =30,num3=40):

print("num1 = ", num1)

print("num2 = ", num2)

print("num3 = ", num3)

return num1 + num2 + num3

print(sum_func(10, 20))

Output:

num1 = 10

num2 = 20

num3 = 40

70

The remaining *args and **kwargs arguments we will see in the next post.


Share:

0 comments:

Post a Comment