Python Function Arguments

python function arguments

 

In python, you can define a generic function which can be used by passing arguments or parameters. Even we use built-in functions by passing its required arguments.

You might have seen print() function used since our first article where we have passed character string to print() function to print “Hello World” message.

 

Arguments

In python function topic, we have learned about how to define functions and calling it. In this article we will see what complications can encounters with python function if we don’t provide proper parameters to the function. Also, we will go through different types of function arguments.

Here is the simple example.

 

 
# Define python function
def Name(FirstName, LastName):
    print("First Name :", FirstName)
    print("Last Name :", LastName)

# Call python function with arguments
Name("Tekkie", "Head")

Output


>>> Name("Tekkie", "Head")

First Name : Tekkie
Last Name : Head
>>>

Observe this example carefully where we have provided all the required arguments while calling the Name() function. Let’s see what happens if we provide only one argument where we actually required two arguments.


 >>> Name("Tekkie")
 
Traceback (most recent call last):
  File "", line 1, in 
    Name("Tekkie")
TypeError: Name() missing 1 required positional argument: 'LastName'
>>>

Or try to call Name() function without any argument.


>>> Name()

Traceback (most recent call last):
  File "", line 1, in 
    Name()
TypeError: Name() missing 2 required positional arguments: 'FirstName' and 'LastName'

1. Python Required Arguments

In this type of argument, you need to provide same number of arguments which are necessary for that function and with its exact datatypes. 

Observe this example where addition() function expect two numeric arguments and see what happens with different test cases.

 
# Define function
def addition(a,b):
    result=a+b
    print("Addition result :", result)

Tast Cases & its Result:

addition()

TypeError: addition() missing 2 required positional arguments: ‘a’ and ‘b’

>>>

addition(10, “Character_String”)

result=a+b

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

>>>

addition(10, 20)

Addition result : 30

>>> 

2. Python Default Arguments

In this type of argument, the parameter values are exactly mapped one-to-one to the variables in the function.

Here is an example of default argument type where we have defined default value for variable – b and passing the value for variable – a. Also observe, what happens if you still provide value for variable – a which you have already defined.

 
# Define function
def addition(a,b=0):
    result=a+b
    print("Addition result :", result)

addition(10)

Addition result : 10

>>>

addition(10, 20)

Addition result : 30

>>>

In case-1, you have just provided one argument which are mapped to first variable –  a and default specified value taken for variable b    (i.e. b=0), hence we got result, 10+0=10

In case-2, we have tried to pass two arguments. first argument mapped to first variable (i.e. a) likewise second argument mapped to second variable (i.e. b) by overwriting its default value zero. Hence we got addition result, 10+20=30

You need to very careful when you define some value for variable in function but at the same time trying to pass value for same variable while calling the function.

3. PYTHON Keyword ARGUMENTS

As discussed in the first two argument types, when we call function with values, these values get assigned to the arguments according to its variable position.

In above example first value always assigned to first variable – a and second value assigned to second variable – b.

Python gives you flexibility to assign values for arguments by specifying its variable name. You could call this function as addition(a=10, b=20) or addition(b=20, a=10). In both the cases you will get the same results.

 
def addition(a,b):
    result=a+b
    print("Addition result :", result)

addition(a=10, b=20)

Addition result : 30

>>>

addition(b=20, a=10)

Addition result : 30

>>>

Also, there is possibility to call function in a mixed way – passing default arguments along with keyword arguments but here you need to keep in mind one important rule.

Rule: When you are calling a function, you must provide all the positional(default) arguments first, then you can pass keyword arguments.

Having a positional (default) arguments after keyword arguments can result into an error. Here are few examples.

addition(10, b=20)

>>> addition(10, b=20)

Addition result : 30

>>>

addition(b=10, 20)

>>> addition(b=10, 20)

SyntaxError: positional argument follows keyword argument

>>>

Here is one very interesting scenario – In case if you attempt multiple arguments refer to same variable then it will result into an error.

addition(10, a=20)

Traceback (most recent call last):

 File “<pyshell#15>”, line 1, in

 addition(10, a=20)

TypeError: addition() got multiple values for argument ‘a’

Here, you have already provided value for variable – a by passing position argument (10) but still you have provided value for variable- a again through keyword argument (a=20) and it ends up with an error.

 

4. Variable Length or Arbitrary Arguments

If you are not sure how many argument values to be passed to the function, then these arbitrary arguments come in picture by defining its variable argument length.

Mostly such type of argument used where we are passing values from outside to the loop statements.

The asterisk * used with variable name (*a) to decide its variable argument length.


def addition(*a):
    sum=0
    for var in a:
        sum=sum+var
    print("Addition Result :", sum)
# call function
addition(10,20,30,15,20)

Output


Addition Result : 95
>>>

In this example we have called function with multiple arguments.  These argument gets wrapped into tuple and values gets assigned to it. Inside function we have used for loop to retrieve those arguments back.

An example which describes combination of default argument and arbitrary argument.


def addition(b, *a):
    sum=0
    for var in a:
        sum=sum+var
    print("Addition Result :", sum)
    print("Value of variable b :", b)

# call function
addition(10,20,30,15,20)

Above program gives following result – 


Addition Result : 85
Value of variable b : 10
>>>

Here, we have assigned value 10 for variable – b and rest arguments are assigned for variable – a and it retrieved back further with for – loop, hence we got the final result – 85

Spread the love

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *