Python Functions

Share:

Functions are collection of codes which can be used multiple times without rewriting it.

                def FuncName(VarName, …):
                                codes

to define a function, we need def keyword following by function name prentices , colon and codes remember that python don’t have any block so indentation is really important here codes related to function must be indented.



Parameter:
Variables inside function prentices are called parameter, defining parameters are optional, parameters are simply variables which take their values during function call.
We can give the function parameters default value so if we leave them empty during function call python will use the default values:

                def FuncName(VarName = Value , …):
                                codes

tuples and dictionary as function parameter:
sometimes we face with situations which we don’t now how many parameters we need or number of parameter depends on different factors so we can use args(*) or kwargs(**) to make tuples or dictionaries which  stores the given values for function parameter:

                def FuncName ( *VarName ):
                                codes

this will store all given values into a tuple while calling a function, then we can access them inside the function.
We can also give the values as dictionary values but for that we will need to make the parameter like below:       

def FuncName ( **VarName ):
                                codes

here the deference comes, first way all we need was to prefix the parameter name with (*) and while calling the function we passed the values normally ( single values ) but when we prefix the parameter name with(**) here we will need to pass the values in shape of keys and values:

                FuncName ( values , …)                                 # *VarName function calling
                FuncName ( key = value , … )                      # **VarName function calling

Keys must be a String without citations, we can use args and kwargs to gather as function parameters.

Storing function in variables:
If we store the function name into a variable, the variable will store the function then we can call the function by that variable, when we do this the variable will store a copy of the function that means if we even delete the original function we still can access it if we assigned it to a variable:

                def FuncName(VarName, …):                     # function declaration
                                codes
                VarName = FuncName                                  # assigning the function name to a variable
                del FuncName                                                   # deleting the real function
                VarName(value, …)                                        # accessing the function using the variable

So now that we know that we can store function in variable I think it is clear that we can use this knowledge and pass function as parameters in other function and call them there.
If we assign the function name with it prentices into a variable the variable will store what function returns.

Lambda functions:
Lambda or anonymous function are inline functions that don’t have a name so they are not reusable:

                lambda parameter, .. : codes

lambda functions are used in places that we need to pass a function name there.

Note: we have the concept of nested function in python that means we can write functions inside other functions which makes them local to the parent function.

Return:
We can use return keyword to return a value from a function, return keyword terminates the function, but we can use yield keyword to return collection of values and the function will return collection of value for that we will need to iterate on it to access its elements:

                def FuncName():
                                for VarName in range(10):
                                                yield VarName
                for VarName in FuncName():
                                print(VarName)

or we can cast the FuncName() into a list and store it in to a variable if needed:

                VarName = list(FuncName())

we can also use next method to access the returned values but we must store the returned values into a variable first:

                VarName = FuncName()
                next(VarName)

Note: returning the collection of values will make the function a generator.

Decorators:

Decorators allow us to pass a function into another function as its parameter value while main function returns the function inside it that calls the outer function:

               def FuncName(VarName):
                                def FuncName1():
                                                VarName()
                               
                @FuncName
                def FuncName2():
                                codes

now if we call the FuncName2 it will call FuncName while passing itself as its parameter value.

No comments