Functions
In programming, the use of function is one of the means to achieve modularity and re-usability. Function can be defined as a named group of instructions that accomplish a specific task when it is invoked. Once defined, a function can be called repeatedly from different places of the program without writing all the codes of that function every time, or it can be called from inside another function, by simply writing the name of the function and passing the required parameters, if any. The programmer can define as many functions as desired while writing the code.
The Advantages of Function: -
Following are the advantages of using functions in a program: Increases readability, particularly for longer code as by using functions, the program is better organized and easy to understand. Reduces code length as same code is not required to be written at multiple places in a program. This also makes debugging easier. Increases reusability, as function can be called from another function or another program. Thus, we can reuse or build upon already defined functions and avoid repetitions of writing the same piece of code. Work can be easily divided among team members and completed in parallel.
User defined Functions: -
Taking advantage of reusability feature of functions, there are large number of functions already available in Python under standard library. We can directly call these functions in our program without defining them. However, in addition to the standard library functions, we can define our own functions while writing the program. Such functions are called user defined functions. Thus, a function defined to achieve some tasks as per the programmer's requirement is called a user defined function.
Creating User Defined Function: -
The items enclosed in "[ ]" are called parameters and they are optional. Hence, a function may or may not have parameters. Also, a function may or may not return a value. Function header always ends with a colon (:). Function name should be unique. Rules for naming identifiers also apply for function naming. The statements outside the function indentation are not considered as part of the function.
The def is the keyword used to define a function and function name is written following def keyword. When it runs, it creates a new function object and assigns it a new name. As it is a statement, so there is no issue in using it inside any control structures like if else.
Arguments and Parameters: -
the numbers were accepted from the user within the function itself, but it is also possible for a user defined function to receive values at the time of being called. An argument is a value passed to the function during the function call which is received in corresponding parameter defined in function header.
Functions Returning Value: -
A function may or may not return a value when called. The return statement returns the values from the function. In the examples given so far, the function performs calculations and display result(s). They do not return any value. Such functions are called void functions. But a situation may arise, wherein we need to send value(s) from the function to its calling function. This is done using return statement. The return statement does the following: • returns the control to the calling function. • return value(s) or None.
The return statement takes zero or more values, separated by commas. Using commas actually returns a single tuple. To return multiple values, use a tuple or list. Returning multiple items separated by commas is equivalent to returning a tuple. Next, we will take examples where a function returns more than one values.
Flow of Execution: -
Flow of execution can be defined as the order in which the statements in a program are executed. The Python interpreter starts executing the instructions in a program from the first statement. The statements are executed one by one, in the order of appearance from top to bottom. When the interpreter encounters a function definition, the statements inside the function are not executed until the function is called. Later, when the interpreter encounters a function call, there is a little deviation in the flow of execution. In that case, instead of going to the next statement, the control jumps to the called function and executes the statement of that function. After that, the control comes back the point of function call so that the remaining statements in the program can be executed. Therefore, when we read a program, we should not simply read from top to bottom. Instead, we should follow the flow of control or execution. It is also important to note that a function must be defined before its call within a program.