1 minute read

There are mainly two types of functions:

  • Normal function - It is is defined by the keyword def and it has a name.

Example:

my_func is de name and we are using the keyword def.

def my_func(a, b, c):
    result = a + b + c
    return result
  • Lambda or anonymous function - Is anonymous because it does not has a name and is defined by using the keyword lambda, this function consist only in one expression(one line of code).

Example:


lambda a, b, c : a + b + c

#Even you can do more calculations
lambda a, b, c : a ** 3 + b + (c % 2)
lambda x : x % 2 == 0

Note: Any value is a expression like number or string.

Additional information

Syntax

lambda parameters: expression

Lambda – This is the keyword used to create a lambda function.

Parameters – It can have zero, one or many parameters. You just need to specify comma to separate the parameters after the lambda keyword.

Expression – This is the body of the function. This is where you specify the operation and this can have only one expression.

Comparison of lambda with a standard function

Standard functions

  • Used many times.
  • Require more lines of code lines of code.
  • None or more returns.
  • Named only.
  • They can have multiple expression/statements in her body.

Lambda function

  • You can use only one time.
  • It requires a maximum of two lines of code.
  • One or more return.
  • Named or anonymous.
  • They can have just a single expression in their body.

References

What is lambda function?

Lambda comparison - video

Lambda syntax