Keyword Argument In Python

aimtocode

Python Function Definition

A Function groups the numbers of program statements into a single unit and gives it a name.

This unit can be called from other part of the program. The purpose of using function is to reduce the size of the program.

The repeated set of statements can be defined as a function and the user can call the function any number of times, anywhere from the program.

Also functions are a key way to define interfaces so programmers can share their code.


Python Keyword Arguments

Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.

This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters.

You can also make keyword calls to the add() function in the following ways −

When the above code is executed, it produces the following result − Sum of 20 and 10 is 30

Output:

 Sum of 20 and  10 is 30

Example:



 sujeet and Prayag are Brothers

Notice in above example, if we had supplied arguments as print_name('Prayag','Sujeet'), the output would have been Prayag and Sujeet are brothers as the values would have been assigned by arguments position.


Required Arguments

Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.

Example:


To call the function Std(), you definitely need to pass one argument, otherwise it gives a syntax error.

When the above code is executed, it produces the following result:

Output:

 Enter the name : Prayag

 Hello : Prayag

Python Default Arguments

A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. The following example gives an idea on default arguments, it add default value of b if it is not passed while calling.




Output:

 3
 6

In the program above, default arguments 2 and 4 are supplied to the function. First, the user has provided the arguments 1 and 2, hence the function prints their sum which is 3. In the second call, the user has not provided the arguments. Thus the function takes the default arguments and prints their sum.

Example 2:


When the above code is executed, it produces the following result:

Output:

 
 Sum of  10 and  20 is 30
 Sum of  10 and  0 is 10

When default argument is used in program, Non default arguments should come before default arguments.


Variable-Length Arguments

You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.

Syntax for a function with non-keyword variable arguments is this



An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call.

Following is a simple example

When the above code is executed, it produces the following result:-

 Output is: 
 20
 Output is: 
 50
 60
 55

Example 2:



Output:

 Prayag
 Aimtocode
 Rahul
 ('Mukesh', 'Java')
 ('Rakesh', 'C++')
 ('Ritesh', 'SQL')


The Anonymous Functions or Lambda Functions

hese functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.

  • Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions.
  • An anonymous function cannot be a direct call to print because lambda requires an expression
  • Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.
  • Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline statements in C or C++, whose purpose is by passing function stack allocation during invocation for performance reasons.

Syntax

The syntax of lambda functions contains only a single statement, which is as follows

 lambda [arg1 [,arg2,.....argn]]:expression

Following is the example to show how lambda form of function works

When the above code is executed, it produces the following result

 Sum is: 15
 Sum is: 80
aimtocode