Function is a group of related statements that can run more than once is a program. Functions help break our program into small chunks. In this way its easier to manage large programs.
Why do we use functions?
Its serves 2 major roles:
- Minimizing the code reuse and redundancy : Functions allow us to package the logic that we want to use it more than once is a program. They help us to reduce the redundancy in the program and hence its easier to check the program.
- Procedural decomposition: It helps to divide the program into small chunks and makes it easier to understand. It assigns one function for each sub tasks in the process. Its easier to implement small tasks in isolation than to implement it in the entire process at once.
def Statements:
It creates a function object and assigns it to a name. General format is
Syntax:
def fun_name(parameters):
“””docstring”””
statements
return value
- The keyword def is used to start a function.
- def creates a new function and assigns a name to it. A function name is given to identify a function. A function name can be a combination of alphabets and numbers but it should always begin with an alphabet an underscore, or a dollar sign ($) . Keywords and spaces cannot be used in function names. They are case sensitive.
- The statement becomes function’s body that is every time the code which the python executes every time the function name is called
- The argument names in the header are assigned to the objects passed in parameters at the point of the call
- The function usually has a return statement. When python’s compiler reaches the return statement it ends the function call and sends the result back to the caller. The function which doesn’t have a return function ‘None’ object automatically but this return is usually ignored at the call.
- Usually python functions are objects they are recorded explicitly in the memory at the program execution time. Functions allow arbitrary attributes to be attached to record the information for later use.
Function definitions and calls
Definition:
Its better to explain this with an example. The function given in the below example defines a function called ‘times’ which returns the product of 2 arguments.
Call :
The def statement makes a function but doesn’t call it. After the def has run you can call the function in your program by adding parentheses

Another Example:

Setting default parameter value
If we assign a value to the function parameter itself and during function call if we give arguments then it will override the assigned value else if we don’t give any arguments then it will take the original values by default.

Lambda functions
Like def ,lambda creates an function to be called later but lambda returns the functions instead of assigning a name to the function (This is why lambdas are also called anonymous function).
Why to use lambda?
- Its a feature that allows us to use in-line function definitions in places where def statements wont work properly.
- Lambda is tend to be used for coding simpler concepts while def handles larger ones.
- Lambda is used to code jump tables (lists or dictionaries of action to be performed on demand)

Difference between lambda and def
- In the above example the lambda builds up a list of 3 functions by embedding lambda expression inside a list literal but a def wont work like this cause def a statement not an expression.
- def is used to handle larger tasks while lambda is used for smaller coding

Nested functions and its scope
In simple words a function inside a function is called as nested functions. If a variable is declared as a global then inside the function using assignment operation we can change the value of the variable.

Nesting lambda in def

Nested lambda:

Author : Nivedita.M
Edited by: Arcot Gautham
