Concepts / Calling a Function

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.

  • 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

Callercurrent statementFunction bodystatements in orderNext statementafter the callcallfinish
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

Callercall locationFunction bodycurrent executionReturn pointnext linetransfer controlreturn control
How are the caller, the function body, and the place to resume connected during a function call?
Worked example

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.

  1. 1
    Reach the call

    The caller reaches the statement that invokes greet with Maya.

  2. 2
    Enter the function

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

  3. 3
    Run the body

    The statements inside greet execute in their written order.

  4. 4
    Finish the function

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

  5. 5
    Resume the caller

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

Result

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

Key point

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.

Call 1MayanameMayaCall 2NoahnameNoahargument valueargument value
How do argument values move into a function's parameters, and what changes when the call supplies different values?
Worked example

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?

  1. 1
    First call

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

  2. 2
    Function execution

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

  3. 3
    Second call

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

  4. 4
    Reuse

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

Result

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

Reuse Without Duplication

Function bodywritten onceCall Afirst useCall Bsecond useCall Cthird useinvokesinvokesinvokes
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

  • Common mistake

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

    What it looks like

    A function has been written, but the program never invokes it.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Leaving out the parentheses when calling a function.

    What it looks like

    Writing the function name without the parentheses used for the call.

    Why it fails

    The source material specifically requires parentheses when calling a function.

    Do this instead

    Use the function name followed by parentheses when invoking it.

  • Common mistake

    Providing the wrong number of arguments.

    What it looks like

    Calling a function with fewer or more argument values than its parameters require.

    Why it fails

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

    Do this instead

    Check 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 like

    Predicting that the statement after a call runs before the called function's statements.

    Why it fails

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

    Do this instead

    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.
Best practice

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.

Related concepts

More concepts starting with C