PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
A function definition describes work that can be performed later. In the running program, printSteps is defined before main, but its three cout statements do not run when C++ reaches the definition. Execution starts in main, so the output statements stay inactive until a printSteps call transfers control into the function body.
#include <iostream>
using namespace std;
void printSteps() {
cout << "Wash" << endl;
cout << "Rinse" << endl;
cout << "Repeat" << endl;
}
int main() {
printSteps();
cout << "Again" << endl;
printSteps();
}The line void printSteps() is the function header. The braces and the three cout statements form its function body. Reading this definition makes printSteps available to main, but it does not produce Wash, Rinse, or Repeat by itself.
If both printSteps(); calls were removed from main, would the three lines inside printSteps appear in the output?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The definition and the call have different jobs. The definition writes void printSteps() and then gives the function body inside braces. Each printSteps(); inside main is a function call, a statement that asks C++ to execute that already-defined body. The parentheses and semicolon are part of the call form, even though nothing is written between the parentheses here.
void printSteps() {
cout << "Wash" << endl;
cout << "Rinse" << endl;
cout << "Repeat" << endl;
}
int main() {
printSteps();
cout << "Again" << endl;
printSteps();
}Execution begins in main and reaches the first printSteps(); statement. Control enters the function body, so the program prints Wash, then Rinse, then Repeat. When the body finishes, control returns to main at the statement immediately after that call. That statement prints Again, so the program does not restart main and does not jump directly to the second call.
After Again is printed, main reaches the second printSteps(); call. Control enters the same body again and prints Wash, Rinse, and Repeat in the same order. Once that call finishes, execution continues to the end of main.
What is the fourth output line of the running program?
printSteps();
cout << "Again" << endl;
printSteps();Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The program has one three-statement function body but executes that body twice. The two calls do not create two copies of the definition. Each call enters the same three statements, waits for them to finish, and then returns to its own position in main.
Wash
Rinse
Repeat
Again
Wash
Rinse
RepeatBecause both calls use the same body, changing one of its three cout statements changes the output produced by both calls. The first call and the second call occur at different points in main, but they perform the same stored sequence. Reusing the definition keeps the repeated behavior consistent without writing the three statements again.