Python Functions

Functions are reuseable 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 function outcome in memory which can be referred by a calling variable. The program control flows back to the function call when a return statement is executed. If 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 shortcut. They can also be anonymus at times.

lambda function syntax

lambda arguments: expression
    >>> add = lambda a, b: a+b
    >>> add(2, 3)
    >>> even = lambda x: x%2==0
    >>> even(22)
    >>> even(23)