Flow of Execution

Execution always begins, at the first statement of the program. Statements are executed one at the time, in order from top to bottom.  Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called. A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the body of the function, then executes all the statements and then comes back to pick up where it left off.
If there is another or second function call inside the first function definition, then execution of the program jumps to the definition of a second function from the first function and then comes back to the first function and executes the first function and after returns the value and finishes the execution of the program. We know it’s a little bit confusing but it’s very common thing that can happen in larger programs.

Programming languages in general are good at keeping track of where it is, so each time a function completes, the program picks up where it left off in the function that called it. When it gets to the end of the program, it terminates. So when you read a program it’s good practice to follow the flow of execution rather than read it from top to bottom. 

Nema komentara:

Objavi komentar