Calling a Function
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.
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?
Reveal 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
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
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.
- 1Reach the call
The caller reaches the statement that invokes greet with Maya.
- 2Enter the function
Execution transfers to the first statement in the greet function's body.
- 3Run the body
The statements inside greet execute in their written order.
- 4Finish the function
When greet reaches the end of its body or a return statement, the function call is complete.
- 5Resume 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.
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?
- 1First call
Book is the argument value supplied to describe, so the function's item parameter receives Book for that call.
- 2Function execution
The same statements in describe execute using the current value of item.
- 3Second call
Bicycle is supplied on the next call, so item receives Bicycle for that call.
- 4Reuse
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
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
Common mistake Treating a function definition as if it automatically runs the function.
What it looks likeA function has been written, but the program never invokes it.
Why it failsDefining a function is only half the process. The function's statements run when execution reaches a call.
Do this insteadCall the function at the place where its behavior is needed.
Common mistake Leaving out the parentheses when calling a function.
What it looks likeWriting the function name without the parentheses used for the call.
Why it failsThe source material specifically requires parentheses when calling a function.
Do this insteadUse the function name followed by parentheses when invoking it.
Common mistake Providing the wrong number of arguments.
What it looks likeCalling a function with fewer or more argument values than its parameters require.
Why it failsArguments are the actual values supplied for the function's parameters, so the call must match the function's parameters.
Do this insteadCheck the function's parameters and provide the correct number of argument values.
Common mistake Assuming execution continues with the next caller statement before the function body runs.
What it looks likePredicting that the statement after a call runs before the called function's statements.
Why it failsControl transfers to the first line of the function body, and the body executes before control returns.
Do this insteadTrace the call into the function body, then resume at the line after the call.
Practice the Trace
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.