DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSC++ FUNDAMENTALS

Functions: A Definition Runs Only When You Call It

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 23

Defining printSteps does not execute its three statements

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.

CPPThe definition appears above main, but its body runs only at the two call sites.
#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.

CHECKPOINT 1Not answered

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 expression printSteps(); is a call, not another definition

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.

CPPThere is one definition and two call statements.
void printSteps() {
    cout << "Wash" << endl;
    cout << "Rinse" << endl;
    cout << "Repeat" << endl;
}

int main() {
    printSteps();
    cout << "Again" << endl;
    printSteps();
}

When a call finishes, execution continues at the next statement in main

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.

The control flow through main and the two calls to printStepsExecution begins at main. The first printSteps(); call transfers control to the printSteps body, which prints Wash, then Rinse, then Repeat. Control continues at the next statement in main, which prints Again. The second printSteps(); call enters the same body and again prints Wash, Rinse, and Repeat. Control then reaches the end of main.main entryprintSteps();AgainprintSteps();end of mainWashRinseRepeatenterresumeenter againresumeprintSteps body
Each call enters the same body, then execution resumes in main.
CHECKPOINT 2Not answered

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.

Calling printSteps twice repeats behavior without copying its body

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.

TEXTThe two calls produce seven output lines in order.
Wash
Rinse
Repeat
Again
Wash
Rinse
Repeat

Because 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.

Previous · Switch StatementNext part · Quiz