Anonymous Functions or Lambda

Anonymous Functions

  • A function without a name is called anonymous function.
  • Sometimes we can declare a function without any name,such type of nameless functions are called anonymous functions or lambda functions.
  • Anonymous functions are also called lambda functions. 

Normal Function:

  • We can define by using def keyword.
                          def  squareIt(n): 
                                  return n*n   

Lambda Function:

  • We can define by using lambda keyword
  • lambda n:n*n

Syntax of lambda Function: 

  • lambda   argument_list : expression  
Note: By using Lambda Functions we can write very concise  code so that readability of the program will be improved.

Q. Write a program to create a lambda function to find square of given number?

Q.  Lambda function to find sum of 2 given numbers?

Q. Lambda Function to find biggest of given values?

Note:-

  • Lambda Function internally returns expression value and we are not required to write return statement explicitly.
  • Sometimes we can pass function as argument to another function. In such cases lambda functions are best choice.
  • We can use lambda functions very commonly with filter(),map() and reduce() functions,b'z these functions expect function as argument.

  

Comments