Functions
User-defined functions :
1. To create a function without parameters:
Syntax:
def function name():
………………..
………………..
Block of statements
…………………..
2. To create a function with parameters:
Syntax:
def function name(arg1, arg2, arg3,………………,argn):
………………..
………………..
Block of statements
…………………..
Calling a Function: (To invoke or access a function)
To call a function, use the function name followed by parenthesis
Syntax: To call a function without parameters
function name()
Syntax: To call a function with parameters
function name(arg1, arg2, arg3,………, argn)
Example 1: Without Parameters:
Parameters to Functions :�
Positional & Keyword Arguments:
Syntax:
lambda arguments: expression
Lambda functions can have any number of arguments but only one expression.
The expression is evaluated and returned. Lambda functions can be used wherever function
objects are required.
Example
A lambda function that adds 10 to the number passed in as an argument, and print the result:
x = lambda a : a + 10�print(x(5))
Example
A lambda function that multiplies argument a with argument b and print the result:
x = lambda a, b : a * b�print(x(5, 6))
Example
A lambda function that sums argument a, b, and c and print the result:
x = lambda a, b, c : a + b + c�print(x(5, 6, 2))
Use of Lambda Function in python
We use lambda functions when we require a nameless function for a short period of time.
In Python, we generally use it as an argument to a higher-order function
(a function that takes in other functions as arguments). Lambda functions are used along
with built-in functions like filter(), map() etc.