Python Function Arguments


In this section we learn how to pass arguments to a function in Python. Consider the following function in which we have passed two arguments to the function and the function adds them storing the result in another variable and returns the result.

CODE

>>> def add(a, b):

          c = a + b

          return c

>>> print(“The sum of a and b is”)

>>> print(add(4, 5))

OUTPUT

The sum of a and b is

9

9

In the above example, a function is defined with a name add that has two arguments. In the function the value after addition of a and b is stored in the variable c and in the next line c is returned. Outside the body of the function first print statement is used to simply print a statement and in the next print statement we called the function by passing the values of the parameters, the function returns the value in the print statement and the value is printed.

If we call the function with different number of arguments an error message will be generated by Python interpreter. If the function was called with one argument the following error will be generated:

EXAMPLE 1

>>> print(add(4))

Traceback (most recent call last):

  File “<pyshell#16>”, line 1, in <module>

    print(add(4))

NameError: name ‘add’ is not defined

EXAMPLE 2

>>> add()

Traceback (most recent call last):

  File “<pyshell#18>”, line 1, in <module>

    add()

NameError: name ‘add’ is not defined

nameerror

The first error occurred because we passed one argument instead of two in function and the error occurred that this function is not defined. It means if the names of two different functions are same we can differentiate them by their number of arguments. In the second error no arguments are passed.

 

Variable function Arguments

In Python we can define functions that can have a number of arguments. There are the following three different forms of functions regarding this type:

 

Python default Arguments

In Python there are functions that have default values as their arguments. A default value to a variable as function argument can be assigned by using the assignment statement (=). Consider the following example:

CODE

>>> def add(a, b=5):

          c = a + b

          print(“The sum of a and b is:”, c)

>>> add(4)

>>> add(3,7)

OUTPUT

The sum of a and b is: 9

The sum of a and b is: 10

a-and-b

In the above example, a function is defined named add that adds two numbers a and b. The variable b is assigned a value 5. When the function is called, we passed one value for the argument the second value for the second argument is optional as it is defined in the function header.

If two values are passed then the second argument will be overwritten and the new value will be printed.

We can say that the variable b has a default value. The arguments to the left side cannot be set as default the Python interpreter will generate an error. Consider the following example:

CODE

>>> def adds(a=4,b):

          c= a+b

OUTPUT

SyntaxError: non-default argument follows default argument

default-argument

 

Python Keyword Arguments

In any programming language when a function is called by passing arguments the values are assigned to the corresponding parameters of the functions. In this way the order in which the values are passed to functions should be the same as the parameters are in function.

In Python we can also call functions using keyword arguments. When a function is called using keyword arguments then the order in which the arguments are written does not matter that is we can change the order or position of the arguments when calling the function. Consider the following example in which functions are called using keywords arguments:

CODE

>>> def add(a, b):

          c =a+b

          print(“The sum of numbers a and b is:”,c)

>>> add(a=3, b=4)

>>> add(b=3, a=7)

OUTPUT

The sum of numbers a and b is: 7

The sum of numbers a and b is: 10

the-sum-of-numbers

In the above example, the function is called two times with keyword arguments. You can see that the order of arguments when calling function using keyword arguments does not matter. The sum is printed without any error.

The interpreter of Python will generate an error if the keyword arguments are not following positional arguments. If the keyword arguments are placed after positional arguments then interpreter will generate error. Consider the following example:

CODE

>>> add(b=5, 4)

>>> add(a=4, 5)

OUTPUT

SyntaxError: positional argument follows keyword argument

SyntaxError: positional argument follows keyword argument

syntaxerror

 

Python Arbitrary Arguments

The arbitrary arguments are used when we do not know in advance that how many arguments are to be passed to the function. The arbitrary arguments can be used by using asterisk (*) before the name of parameter. Consider the following example:

CODE

>>> def company(*employees):

          for i in employees:

                   print(“Welcome”, i)

>>> company(“David”, “John”, “Stuart”)

OUTPUT

Welcome David

Welcome John

Welcome Stuart

welcome-stuart

In the above example, we used an arbitrary argument as function parameter because we did not know that how many names of the employees are going to be printed. The counter variable of the loop counts through the tuple “employees”. And the counter variable is printed which prints each element in the tuple. The tuple is defined in function calling.