FUNCTIONS:

A function in a programming world is defined as a set of statements combined together to accomplish a specific task. Functions usually take data as input, process it, and return the result. They allow reuse of the code and avoids rewritng. If a single task has to be performed multiple times, we can write that task in a function once and call it multiple times instead of rewriting the same code.

The syntax of the functions in Python is as follows:

                def function_name(input parameters):
                    """
                    Description of the function.
                    """
                    statement1
                    statement2
                    ...
                    statement(n)
                    return result
            

The Syntax Explanation:

The first line of a function will be it's header begins with the keyword "def" followed by the name of the function and a paranthesized list of input parameters and is ended with the symbol ":" then begins the function body. The body of the function is identified by using the indentation. Generally the first line of the function body contains a string explaining the description about the finction which is called the docstring and can be accessed as function_name.__doc__ . Though the docstring is optional it is recommended to have docstring for every function.

The function body must contain one or more python statements and an optional return statement to return a value from the funtion. The return is the end of the function and if no return statement is present the last statement in the body will be treated as the end of function. A function will always return a result by using the return statement and if this statement is not present it will return the value None.

The following example explains the usage of a simple function in python:

                def sum(number1, number2):
                    ''' Adds two numbers '''
                    return number1 + number2
            

The Parameters:

  The parameters list may contain none or more parameters. Parameters can be mandatory or optional. The optional parameters must follow the mandatory parameters. A funtion always returns something by using the return statement. If the return statement is not present the function returns the value None.

The following example describes the usage of mandatory parameters in a funcion:

        def sum_of_integers(integer1, integer2):
            return integer1 + integer2

The above function must need two parameters integer1 and integer2 for invoking it.

The following exmple describes the usage of optional parameters in a funciton:

        def request_food(item = "pizza"):
            print(item)

In the above example when the function request_food is called without specifying the item "pizza" will be printed. If an item is specified the item name will be printed on the screen.

Combination of optional and mandatory parameters:

    def restaurant_bill(burgers, pizzas, icecreams=1):
        sum = (burgers * 15) + (pizzas * 20) + (icecreams *10)
        return sum

When the above function is called the parameters burgers and pizzas are must be specified as they are mandatory parameterss. But the parameter icecream is optional aand will have a default value of 1 if not specified. We must note the optional parameters must follow the mandatory parameters.

Variable length arguments & Variable length keyworded arguments:

These are generally referred as *args and **kwargs allow you to pass a variable number of arguments to a function. What does variable mean here is that you do not know before hand that how many arguments can be passed to your function by the user so in this case you use the keyword *args. *args is used to send a non-keyworded variable length argument list to the function. Here’s an example to help you get a clear idea:

            def sum_of_squares(*numbers):
                '''Calculates the sum of the squares of the given numbers'''
                sum_of_squares = 0
                for number in numbers:
                    sum_of_squares += number * number
                return sum_of_squares

The Program flow with a function:

When a function is invoked the program execution leaves the current section of code and begins to execute the first line inside the function.

The flow will be as shown below:

  • The program comes to a line of code containing a "function call".
  • The program enters the function (starts at the first line in the function code).
  • All instructions inside of the function are executed from top to bottom.
  • The program leaves the function and goes back to where it started from.
  • Any data computed and RETURNED by the function is used in place of the function in the original line of code.