Python Function


Functions are reusable and callable code structures. Functions have their own namespace also known as local.


Function Definition


A function definition has function_name with parameters. Function names generally follow snake_case or camelCase conventions. def is the keyword used to define function.

def function_name(parameters, ...):
    ...


Return Keyword

return is used to return the functional outcome in memory which can be referred to by a calling variable. The program control flows back to the function call when a return statement is executed. If a function has no return statement or it is never executed the return value is None.


Lambda Function


lambda is a keyword in Python. Like def it is also used for function definition. lambda functions are always one-liner function shortcuts. They can also be anonymous at times.


lambda function syntax

lambda arguments: expression

    >>> add = lambda a, b: a+b
    >>> add(2, 3)
    5
    >>> even = lambda x: x%2==0
    >>> even(22)
    True
    >>> even(23)
    False