Concepts / Return Values and Function Output

Return Values and Function Output

A function definition stores the function but does not run its internal statements. Those statements run only when the function is called.

  • Programming

Following the Execution Path

A program can contain function definitions near its beginning and function calls later on. Reading the program from top to bottom is not always enough to predict the immediate next output, because a function call temporarily moves execution into the function body. The key is to distinguish between storing a function definition and executing the statements inside that function.

Key point

A function definition stores the function but does not run its internal statements. Those statements run only when the function is called.

First statementgreet definitionstore functionfarewell definitionstore functionProgram startsgreet callgreet bodyNext statementreadcontinuecontinuecontinuejumpreturn
What statement executes next when the program reaches a definition and later reaches a call?

Definitions Wait for Calls

When execution reaches a function definition, the definition itself is handled, but the statements inside its body are not executed at that moment. The function is stored so that it can be used later. Execution then continues with the next statement after the definition. This is why a print statement inside a function does not automatically produce output merely because the function has been defined.

greet definitiongreet callgreet statementsgreet statementsstoresexecutes
What happens to the statements inside a function when its definition is encountered, and when do they actually run?

Jumping Into a Function and Returning

Worked example

Tracing greet and farewell

A program defines greet and farewell. Later it prints Program starts, calls greet for Alice, prints Between calls, calls farewell for Alice, and prints Program ends. What is the execution order?

  1. 1
    Definitions

    The program first reaches the greet definition and then the farewell definition. Both functions are stored, but the print statements inside their bodies do not run yet.

  2. 2
    First ordinary statement

    The program reaches the statement that prints Program starts, so that output appears first.

  3. 3
    First call

    The call to greet for Alice moves execution into greet. Its two statements run in order, producing Hello, Alice and then Welcome!.

  4. 4
    Resume after the call

    After greet finishes, execution returns to the statement immediately after the call. That statement prints Between calls.

  5. 5
    Second call

    The call to farewell for Alice moves execution into farewell. Its single statement runs and produces its output.

  6. 6
    Final statement

    When farewell finishes, execution returns to the next statement, which prints Program ends.

Result

The output order is Program starts, Hello, Alice, Welcome!, Between calls, the farewell output, and Program ends.

ProgramgreetNext statementcallcompleteresume
How does execution move from the calling statement into the function and then return to the statement after the call?
greet callAlicegreet bodyHello, AliceBetween callsWelcome!entersproducesthen producesreturns to
How does output produced inside a function become visible before later statements run?

Multiple Functions in Order

With multiple functions, definitions still do not produce the functions' internal output. The visible order begins when ordinary statements and calls are reached. At a call, all statements in that function body run in order. Only after the function finishes does the program continue with the statement immediately after the call. A later function call therefore cannot run before an earlier statement that appears before it in the execution path.

Program startsgreet callgreet outputBetween callsfarewell callfarewell outputProgram endsnextenterresumenextenterresume
In what order do statements run when a program defines multiple functions and calls them at different points?
Statement 1Statement 2Statement 3Statement 4nextnextnext
How does the execution position move from one statement to the next when no function call changes the immediate flow?

Common Tracing Mistakes

  • Common mistake

    Treating a function definition as though it immediately executes the function body.

    What it looks like

    Counting the output from greet while the program is still reading the greet definition.

    Why it fails

    Function definitions store the function but do not run the statements inside it.

    Do this instead

    Wait until the execution path reaches a call to greet before counting its internal statements.

  • Common mistake

    Continuing to the next statement immediately after a function call.

    What it looks like

    Putting Between calls before Hello, Alice and Welcome! after the program calls greet.

    Why it fails

    A call moves execution into the function body. The next statement outside the function runs only after the body completes.

    Do this instead

    Trace every statement in the called function in order, then resume at the statement immediately after the call.

  • Common mistake

    Assuming function definitions change the top-to-bottom order by running their bodies early.

    What it looks like

    Expecting farewell output before Program starts because the farewell definition appears near the top.

    Why it fails

    The definition is encountered near the top, but its internal statement is not executed until farewell is called.

    Do this instead

    Separate the order in which definitions are stored from the order in which calls execute their bodies.

  • Common mistake

    Forgetting where execution returns after a function finishes.

    What it looks like

    Tracing execution back to the beginning of the program after greet completes.

    Why it fails

    The program remembers where the call came from and resumes at the exact line immediately after that call.

    Do this instead

    Mark the calling statement and continue from the next statement after it.

Best practice

When tracing a program, make two passes. First, mark function definitions as stored without executing their bodies. Second, follow the ordinary statements from top to bottom, expanding each function call into the ordered statements inside that function and then returning to the next statement after the call.

Practice the Trace

MEDIUM

A program defines two functions. The first function has two output statements, and the second function has one output statement. After both definitions, the program produces a starting message, calls the first function, produces a message between the calls, calls the second function, and produces an ending message. Without running the program, write the order in which the messages appear.

Hints
  • Do not count output from either function while reading its definition.
  • At the first call, list both statements in the first function before moving to the message between the calls.
  • After the second function finishes, continue with the ending message.

What do you think happens?

Which event happens immediately after the first called function completes?

  • The program starts over at the first function definition

  • The next statement after the function call runs

  • The second function definition runs its body automatically

  • All remaining function bodies run before the next statement

Reveal answer
Answer:

The next statement after the function call runs.

A function call moves execution into the function body. When the body completes, execution returns to the exact line immediately after the call.

Execution Rules to Remember

  1. 1
    Execution begins with the first statement and normally proceeds from top to bottom.
  2. 2
    A function definition stores the function; it does not execute the statements inside the function body.
  3. 3
    The statements inside a function run only when the function is called.
  4. 4
    A function call moves execution into the function body, and execution then returns to the statement immediately after the call.
  5. 5
    To predict output, trace definitions as stored, expand calls into their body statements, and preserve the order of all statements along that path.

Key Takeaways

  • Flow of execution is the order in which statements are executed from top to bottom.
  • Function definitions do not execute their internal statements immediately.
  • A function call temporarily moves execution into the function body.
  • After the function body finishes, execution resumes at the statement after the call.
  • Multiple function calls are traced by preserving the order of ordinary statements and expanding each call at the point where it occurs.

Related concepts

More concepts starting with R