Note 3.7.1.
The order of execution is not necessarily the order in which functions are defined! For example, the last function that you write might be the first one that you call in the
main
function.
main
function.
main
, regardless of where it is in the program** (often it is at the bottom). Statements are executed one at a time, in order, until you reach a function call. Function calls are like a detour in the flow of execution. Instead of going to the next statement, you go to the first line of the called function, execute all the statements there, and then come back and pick up again where you left off.
main
, we might have to go off and execute the statements in threeLine
. But while we are executing threeLine
, we get interrupted three times to go off and execute newLine
.
newLine
completes, the program picks up where it left off in threeLine
, and eventually gets back to main
so the program can terminate.
#include <iostream>
using namespace std;
void newLine() {
cout << endl;
}
void threeLine() {
newLine ();
newLine ();
newLine ();
}
int main() {
cout << "First Line." << endl;
threeLine ();
cout << "Second Line." << endl;
return 0;
}
newLine, threeLine, main
newLine, threeLine, newLine, newLine, newLine, main
main, threeLine, newLine, newLine, newLine
main, threeLine
newLine
is called inside of threeLine
.#include <iostream>
using namespace std;
void yo() {
cout << "yo, ";
}
void hello() {
cout << "hello, ";
yo();
yo();
}
void goodbye() {
yo();
hello();
cout << "goodbye,";
}
int main() {
cout << "welcome, ";
goodbye();
return 0;
}
hello
also calls yo
.goodbye
calls other functions that print output as well.yo
in hello
and both of those in goodbye
produce this output.main
also prints something directly.