Concepts / Return Values and Function Output

Return Values and Function Output

Flow of execution is the order in which statements are executed, always proceeding from top to bottom.

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

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

readcontinuecontinuecontinuejumpreturnFirst statementgreet definitionstore functionfarewell definitionstore functionProgram startsgreet callgreet bodyNext statement
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.

storesexecutesgreet definitiongreet callgreet statementsgreet statements
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

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?

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.

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

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

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

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

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

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

callcompleteresumeProgramgreetNext statement
How does execution move from the calling statement into the function and then return to the statement after the call?
entersproducesthen producesreturns togreet callAlicegreet bodyHello, AliceBetween callsWelcome!
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.

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

Common Tracing Mistakes

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

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

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

  • Continuing to the next statement immediately after a function call.

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

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

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

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

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

  • Forgetting where execution returns after a function finishes.

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

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

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. Execution begins with the first statement and normally proceeds from top to bottom.
  2. A function definition stores the function; it does not execute the statements inside the function body.
  3. The statements inside a function run only when the function is called.
  4. A function call moves execution into the function body, and execution then returns to the statement immediately after the call.
  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.