Concepts / Calling a Function

Calling a Function

A function call transfers execution to the function's code block and returns to the caller when the function finishes.

  • Programming

From Definition to Use

Defining a function creates a reusable block of behavior, but the function does not perform that behavior merely because it has been defined. The program must call the function. A function call transfers execution to the function's code block. When the function finishes, execution returns to the place where the call occurred.

What do you think happens?

A program reaches a function call on one line. What happens next?

  • The program immediately continues with the next line after the call.
  • The program transfers execution into the function body, then returns after the function finishes.
  • The program permanently stops at the function call.
Reveal answer

Answer: The program transfers execution into the function body, then returns after the function finishes.

Execution pauses at the call, follows the statements in the function body in order, and returns to the line immediately after the call when the function reaches its end or a return statement.

The Execution Journey

callfinishCallercurrent statementFunction bodystatements in orderNext statementafter the call
What happens when execution reaches a function call, moves through the function body, and returns to the caller?

Think of the caller as the part of the program currently running. When it encounters a function call, execution does not move to the next line immediately. Control transfers to the first statement inside the function. The statements in the function body run in order. After the function reaches the end of its body or executes a return statement, control moves back to the line directly after the call.

The Return Point

transfer controlreturn controlCallercall locationFunction bodycurrent executionReturn pointnext line
How are the caller, the function body, and the place to resume connected during a function call?

Tracing a Greeting Call

Suppose a program calls a function named greet with the argument value Maya, and a later statement displays a completion message. Trace the order of execution.

Reach the call: The caller reaches the statement that invokes greet with Maya.

Enter the function: Execution transfers to the first statement in the greet function's body.

Run the body: The statements inside greet execute in their written order.

Finish the function: When greet reaches the end of its body or a return statement, the function call is complete.

Resume the caller: Execution returns to the statement immediately after the call, where the completion message can run.

The order is caller reaches greet, greet runs, greet finishes, and the caller resumes after the call.

A function call is a temporary transfer of control, not a permanent jump away from the caller. The caller has a place to resume, and execution continues there after the function finishes.

Moving Values into Parameters

Parameters allow a function to accept input values. The parameter is the named input described by the function, while an argument is the actual value supplied in a particular function call. This lets one function body operate with different input values on different calls.

argument valueargument valueCall 1MayanameMayaCall 2NoahnameNoah
How do argument values move into a function's parameters, and what changes when the call supplies different values?

One Body, Two Inputs

A function named describe accepts a parameter named item. The program calls describe first with Book and later with Bicycle. What changes between the calls?

First call: Book is the argument value supplied to describe, so the function's item parameter receives Book for that call.

Function execution: The same statements in describe execute using the current value of item.

Second call: Bicycle is supplied on the next call, so item receives Bicycle for that call.

Reuse: The function body is not rewritten; it is invoked again with a different argument value.

The function body stays the same, while the value available through its parameter changes from Book to Bicycle.

Reuse Without Duplication

invokesinvokesinvokesFunction bodywritten onceCall Afirst useCall Bsecond useCall Cthird use
How can one function body serve several places in a program without repeating the same implementation?

The same function can be called many times from different places in a program. Each call transfers execution to the function's body, and each call can provide its own argument values. This avoids writing the same behavior repeatedly. Keeping the behavior in one function also makes the program easier to maintain because the reusable behavior has one implementation.

Mistakes with Function Calls

  • Treating a function definition as if it automatically runs the function.

    Defining a function is only half the process. The function's statements run when execution reaches a call.

    Fix: Call the function at the place where its behavior is needed.

  • Leaving out the parentheses when calling a function.

    The source material specifically requires parentheses when calling a function.

    Fix: Use the function name followed by parentheses when invoking it.

  • Providing the wrong number of arguments.

    Arguments are the actual values supplied for the function's parameters, so the call must match the function's parameters.

    Fix: Check the function's parameters and provide the correct number of argument values.

  • Assuming execution continues with the next caller statement before the function body runs.

    Control transfers to the first line of the function body, and the body executes before control returns.

    Fix: Trace the call into the function body, then resume at the line after the call.

Practice the Trace

EASY

A program reaches two calls to a function named notify. The first call supplies the argument value Start, and the second supplies the argument value Finish. For each call, describe the movement of execution and identify what changes between the calls.

Hints
  • Begin at the caller's line containing the call.
  • Move to the first statement in the function body and follow its statements in order.
  • Return to the statement after the call.
  • The function body is reused; compare the argument values supplied to its parameter.

When tracing a call, write the execution path in four stages: reach the call, enter the function body, finish the body, and resume after the call. When inputs are involved, separately record the argument value supplied by each call and the parameter that receives it.

Key Takeaways

  • A function call transfers execution from the caller to the function body.
  • The statements in the function body execute in order before control returns to the line after the call.
  • Parameters receive input values, while arguments are the actual values supplied during a call.
  • The same function can be called repeatedly with different arguments, reducing code repetition and improving maintainability.
  • A correct call uses parentheses and the correct number of arguments for the function's parameters.