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.
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
Sum of 20 and 10 is 30
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 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.
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:
Enter the name : Prayag Hello : Prayag
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.
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.
When the above code is executed, it produces the following result:
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.
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
Prayag Aimtocode Rahul ('Mukesh', 'Java') ('Rakesh', 'C++') ('Ritesh', 'SQL')
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.
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