CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Types of Formal Arguments in Python Functions (Class 12 Computer Science)

Types of Formal Arguments

Python Function can have two types of Arguments / Parameters, First is Actual Arguments and Second is Formal Arguments. In this post I have discussed different types of Formal Arguments. 

Types of formal arguments in Python Functions

Variables that we use in function definition to receive the values coming from function call, is called Formal Arguments. Sending the arguments from Function Call to Function Definition is called argument passing in python. There are 4 different types of formal arguments as shown below in the diagram. 
Types of Formal Arguments
Types of Formal Arguments

Following are the different ways of passing arguments to a function in Python:

  1. Positional arguments (Required arguments)
  2. When the function call statement matches the number and order of arguments as defined in the function definition, this is called positional argument.

    In the below example square(x) is taking one argument, here 'x' is an formal argument. So, at the time of function call we have to pass one argument that will be copied to 'x'.

    This example will give error because we are not passing any parameter at line number 6.   
    1. #error
    3. def square(x):
    4.     z=x*x
    5.     return z
    6. r=square()
    7. print(r)

    To remove this error we must pass an argument at line number 6. This is called a required argument, because without it the program will not execute.
    These are also called Positional Arguments, because if we have more than one argument in function, then the formal arguments receive the values in the same order in which they are passed at the time of function call.

    def square(x):
       return z

  3. Default arguments 
  4. Sometime as a programmer if you want to give default values for some of the arguments, then it can be don with the help of Default Arguments. This will assumes a default value if a value is not provided to argument.

    1. def sum(x=3,y=4):
    2.    z=x+y
    3.    return z
    4. r=sum()
    5. print(r)    #Output: 7
    6. r=sum(x=4)
    7. print(r)    #Output: 8
    8. r=sum(y=45)
    9. print(r)    #Output: 48
    #default value of x and y is being used when it is not passed

    In the above given example you can understand the Default Arguments very clearly.
    • At line number 4 I have called sum( ) function without any argument. So, both the default values of 'x' and 'y' taken by the function sum( ) and the output is coming 7(x=3, y=4).
    • At line number 6 I have called sum( x=4 ) with parameter 'x' only. For 'y' the default value will be taken i.e. 4. So the out will come 8.
    • At line number 8 I have called the function sum( y=45 ) with parameter 'y' only. For 'x' the default value 3 will be taken, and the result will be 48.  

  5. Keyword Argument
  6. In some cases when we know the function and it's parameters name too, but we don't know the order in which they are written in function definition. So, it will be very difficult to pass the values to those parameters from function call.

    In this case the positional parameters will not work, because we don't know the exact order of parameters. 

    To pass the parameters in this case we use Keyword Argument. In this case the caller identifies the arguments by the parameter name. Since we know the parameters name, so we case provide them the values in any sequence or order.

    1. def fun( name, age ):
    2.     print ("Name: ", name)
    3.     print ("Age ", age)
    4.     return
    5. # Now you can call function
    6. fun( age=15, name="mohak" )
    7. # value 15 and mohak is being passed to relevant
    8. # argument based on keyword used for them. 

    In the above case at line number 6 we are calling the function fun( age=15, name="mohak" ), but our function definition at line number 1 fun( name, age ) do not have the same order of argument as it is passed at line number 6. 

    In this case the parameters will be passed as keyword arguments. We can give our parameters in any order but the name of parameters should be specified at the time of function call as we are doing at line number 6. 

  7. Variable-length arguments
  8. Till now we have seen all the types of arguments. If you have gone through all the above types of arguments, you must have a question that how do you pass variable length arguments in python? 

    That means in some cases as a programmer you do not know the exact number and types of arguments that is required to be passes when you call the function. In this case you have to use variable length arguments

    By using this you can receive multiple values with single argument name. Also you can call function multiple times with different arguments. The following code explains the variable length arguments:

    1.  def sum( *vartuple):
    2.      s=0
    3.      for var in vartuple:
    4.          s=s+int(var)
    5.      return s
    6.  r=sum( 70, 60, 50 )
    7.  print(r)
    8.  r=sum(4,5)
    9.  print(r)
    10. #now the above function sum() can sum n number of values

    You can understand the use of variable length arguments by the above example. At line number 1 I have defined a function sum( *vartuple) here *vartuple is a variable length argument. vartuple can receive any number of arguments passed at the time of function call as tuple data type.


    • You can also pass variable length keyword arguments you can receive it in **kwarg variable in function definition.
    • kwarg then will receive the data in the form of dictionary.
    • You must notice that for positional arguments I have used single (*) and for keyworded arguments I have used two stars (**).

Using multiple argument types together

Till now we have discussed 4 different types of arguments. Out of four, default argument is used at the time of function definition, we pass other three arguments at the time of function call.

Let's discuss what will be the order of parameters in a function, if we use multiple argument type's together.

Order of these three different Types of Arguments: 
  1. Positional arguments
  2. Keyword arguments
  3. Variable length argument
First All the Positional arguments then all keyworded arguments at the end Variable length argument should come.

Now we will try to understand it with the help on an example. In the below given function prin and cc are positional argument (required argument) and time and rate are default argumentsConsider the following function:

def Interest( prin, cc, time = 2, rate = 0.09):
      return prin * time * rate

Look at the following function call statements: (Green - Correct, Red - Wrong)

1. Interest(prin = 3000, cc = 5)
2. Interest(rate = 0.12, prin = 5000, cc = 4)
3. Interest(cc = 4, rate = 0.12, prin = 5000)
4. Interest(5000, 3, rate = 0.05)

5. Interest(rate = 0.05, 5000, 3)
6. Interest(5000, principal = 300, cc = 2)
7. Interest(500, time = 2, rate = 0.05)

All the function calls in green are Correct, and all the function call in red are Wrong. 

  1. In the first three function call statement, all the parameters are keyword arguments. We are passing print and cc other two arguments time and rate will be taken by default.  Hence this function call is correct.
  2. In Second call also all the parameters are keyword arguments. Only time is not given, its value will be taken with default parameter. So, this will also a correct function call.
  3. In third case, all the parameters are keyword argument so the order of them does not matter. Value of time parameter will be taken by using default parameter.
  4. In fourth case, there are positional arguments and keyword arguments. Now the order matters. Since all the positional arguments are coming before keyword arguments, this is a correct function calling. 5000 and 3 will be assigned to prin and cc respectively. Value of time will be taken using default value.
  5. Fifth case is wrong because order of parameters are not following. Hence it is wrong function calling.
  6. Since prin and cc are positional argument (required argument), its value must be given at the time of function call. In fifth case value of prin is missing, instead of it principal is given which is wrong parameter name. Hence it is wrong function calling. 
  7. In seventh case, 500 will be assigned to prin. Now one positional argument cc is missing. Hence it is wrong function calling.  

What are the different types of actual arguments in function?

Till now we have discussed different types of Formal arguments. Actual Parameters / arguments are given at the time of function definition. Both actual and formal arguments are related to each other. That is way we can call a function using four different types of arguments and we can receive the arguments in four different ways.   

1 comment: