Functions are basically the life-blood of a lot of Python programs. We will help you learn about it step by step to avoid confusion and ensure clarity. Python’s in-built functions of print () and input (). We make use of the first one for output information to the terminal. Moreover, we make use of the second one for getting input from the terminal. In this article, we will learn about the flow of execution and functions in detail.
A function basically performs a particular operation on its arguments and generates a predictable output. To help you understand it better, please take a look at the example of an ATM machine.
If you notice an automated teller machine, you will see how it offers money that is approved by the respective bank. This machine has a function that allows it to give you money on the basis of multiple different inputs.
These inputs include your card, pin number, amount of cash needed, availability of cash in the ATM, denomination notes and more. However, the deal is that the ATM is a little more complicated than it actually seems.
Nonetheless, it will serve as a great example here. When you use the ATM every time, you make its use for withdrawing cash. Meaning to say, you use the same function. Thus, if the input remains the same then the output will also be as expected the same.
For instance, if everything works, you will get the cash. If the machine is empty, you will not get the cash. Meaning to say, if the ATM starts behaving randomly, it will be of no use to anyone.
Coming back to what we are discussing presently, it is safe to say that finding the product of two numbers is a function. So, a Python function that accepts two numbers as input arguments multiplies them and then returns the product as the output is referred to as a function.
Browse more Topics Under Defining Functions
- Invoking Functions
- Passing Parameters
- Scope of Variables
- Void Functions and Functions Returning Values
Flow of Execution
The flow of execution basically refers to the order in which statements are executed. That is to say, execution always starts at the first statement of the program. Moreover, statements execute one at a time. It happens in order, from top to bottom.
Further, functions definitions do not alter the flow of execution of the program. However, it remembers the statements inside the function do not execute until the functions is called.
Moreover, function calls are similar to a bypass in the flow of execution. Thus, in place of going to the next statement, the flow will jump to the first line of the called function. Then, it will execute all the statements there. After that, it will come back to pick up where it left off.
It is essential to remember that when reading a program, do not read it from top to bottom. Instead, keep following the flow of execution. This will ensure that one reads the def statements as they are scanning from top to bottom.
However, one must skip the statements of the function definition until they reach a point where that function is called.
FAQ on Flow of Execution
Question 1: Where does execution start in Python?
Answer 1: The main function is like the entry point of a program. But, the Python interpreter runs the code right from the first line. Thus, the execution of the code begins from the starting line proceeds line by line.
Question 2: What is a Python library?
Answer 2: Python Libraries refer to a set of useful functions that eliminate the requirement to write codes from scratch. Moreover, there are over 137,000 python libraries in existence today. Consequently, Python libraries play an essential role in the development of machine learning, data visualization, image and data manipulation applications, data science, and more.