CBSE CS and IP

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

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

Types of Arguments / Parameters in Python Functions

DataCamp

In Python Functions, we define Functions by using the def keyword. After writing def keyword we have to provide function name, it will have parenthesis after it. This may contain Parameter / Arguments.
def square( <Parameters> ):
    .
    .
    <function body>    
    .
    .

square ( <argument> )

In the above example, you can understand where we have to provide parameters in Python Functions. Square is a function and after it we have to give small brackets that notifies this is a function, inside these brackets we have to provide Parameters. 
We can provide various parameters by separating them with comma. ex:- Parameter1, Parameter2,...., etc.

Difference between argument and parameter in Python Function 

Before starting with the Types of Arguments / Parameters in Python Functions let us discuss what the difference between Argument and Parameter and what is the purpose of Arguments and Parameters.

So, as the sample code snipped is depicting, the variable in function or method definition is called Parameter.  The actual values that we pass at the time of function call are called Arguments

But the interesting thing is that we can use these name interchangeably. So, in this article and the upcoming articles, I will use these name interchangeably.

How many parameters can a function have in Python?

Now you must have the question of how many parameters can function have? So the answer is 253. The maximum number of Arguments and their Corresponding Parameters is 253.

How do you pass multiple parameters in Python?

As we have discussed that python allows you to pass multiple arguments to a function, the question arises that how to pass multiple arguments? So, there are different types of arguments in Python using which you can accomplish this task. 

Types of arguments / parameters

There are two types of arguments, one that is used with function definition another that is used at the time of function call:

def square( x ):  # x is Formal Argument
    z = x * x
    return z
    
r = square( 5 )   # 5 is Actual Argument
print(r)

Output:
25

1. Actual Arguments / Parameters-:

Parameters that are used during a function call is called Actual Argument. As per the programing example given above '5' is an actual argument, because we are giving it at the time of function call.  

2. Formal Arguments / Parameters-:

Formal parameters are those that are used in the function definition and a function declaration. As per the example given above 'x' is the Formal Argument. It will get the value from the function call. So, the '5' will be assigned to 'x', when the function square(5) is called.

Types of formal arguments

There are 4 types of formal arguments.
  • Positional Arguments (Required arguments): When the position of the parameter in function definition matches with the number and order of the arguments in the function call. Then they are called positional arguments.  
  • Default Arguments: When we do not provide value for any of the argument at the time of function call, we can still have a default value in the function definition parameters. 
  • Keyword argument: At the time of function call, if we do not want to provide the arguments in the same order as we have given in function definition. We have to provide it using Keyworded Arguments. In this type of argument passing, we provide an argument with its name and value.  
  • Variable-length argument: This is a very important type of parameter, where we can receive a variable length of arguments. If we are not sure about the number of arguments that are going to come to the function parameters through a function call, we receive the arguments in Variable-length arguments.
For Details Explanation --> Click Here


Data Type of python function arguments

As we know all the data types are object in Python, we can pass any data type as an argument in the Python function. At the time of function call, we have to provide the Data as Actual Argument. In the actual argument, we can provide any data type of argument.

For Practical knowledge see the below video:

No comments:

Post a Comment