Concepts / Multiple Parameters and Arguments

Multiple Parameters and Arguments

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

  • Programming

From Call to Return

A function definition describes a reusable block of code, but defining that function does not execute its body. Execution begins when the program encounters a function call. At that point, the program pauses at the call, transfers control to the function's code block, executes the statements inside it in order, and then returns to the line after the call.

callfinish and returnCallerFunction bodyNext line
What happens after a function is called, and when does control return to the caller?

Following One Invocation

A greeting function called twice

Imagine a function named greet that accepts one parameter named person. A program calls greet with the argument Ada, then later calls greet with the argument Lin.

First call: Execution reaches the call with Ada. Control transfers to the function body, and the parameter person receives the argument value Ada.

First execution: The function body runs using the value currently held by person. When the function finishes, control returns to the line after the first call.

Second call: Execution later reaches the same function again with Lin. This starts another execution of the function body, with person receiving Lin for this call.

Second return: After the second execution finishes, control returns to the line after the second call.

The same function body is executed once for Ada and once for Lin. Each call has its own argument value and its own call, execute, return cycle.

invokefinishcontinueinvokefinishCall with AdagreetFunction runperson = AdaReturncaller continuesCall with LingreetFunction runperson = LinReturncaller continues
How can one parameterized function produce different results without duplicating its function code?

A repeated call is not a continuation of the previous function execution. Each call triggers a fresh execution of the function's code, after which control returns to the caller.

Parameters and Arguments

A parameter is a variable declared in a function's parentheses to receive input. An argument is the actual value supplied in a function call. Parameters describe the inputs a function can receive; arguments provide the values for a particular invocation.

TermWhere it appearsPurpose
ParameterFunction definitionNames a variable that receives input
ArgumentFunction callProvides the actual value for that invocation
declaressuppliesmatches during callFunction definitionparametersFunction callargumentpersonreceiving variableAdasupplied value
What is the difference between named inputs in a function definition and actual values supplied during a call?

Matching Multiple Inputs

When a function declares multiple parameters, the arguments in a call are matched to those parameters in order. The first argument supplies the first parameter, the second argument supplies the second parameter, and so on. The parameters then act as local variables inside the function, holding the values supplied by that call.

Two inputs for one calculation

Imagine a function named combine that declares two parameters: first and second. One call supplies the arguments 12 and 5.

Read the definition: The function declares two receiving variables, first and second.

Match by position: The first supplied value, 12, is matched to first. The second supplied value, 5, is matched to second.

Execute the body: The function body runs with first holding 12 and second holding 5.

Return to the caller: When the function finishes, control returns to the line after the call.

The order of the arguments determines which value each parameter receives.

matches in ordermatches in order12argument 1firstparameter 15argument 2secondparameter 2
How does each argument correspond to the correct parameter inside a function?

Count the parameters before calling a function, then provide the corresponding number of arguments in the intended order. Always include parentheses when calling a function.

Reuse Without Repetition

The main benefit of parameters becomes visible when one function is called from different places with different values. The function's code is written once, while each call supplies the inputs needed for that situation. This makes the program shorter and easier to maintain. If the function's logic needs a correction or improvement, the change is made in the function definition rather than in every repeated copy.

suppliessuppliesCallvalue AdaCallvalue LinpersonAdapersonLin
What changes inside the function when the same function is called with different argument values?

The function body does not need to be duplicated for each input. A new call supplies new arguments, starts a new execution, and uses the same function code with those values.

Mistakes with Calls

  • Writing a function name without parentheses when intending to call it.

    A function call requires parentheses.

    Fix: Use the function name followed by parentheses, supplying arguments inside them when parameters are declared.

  • Providing the wrong number of arguments.

    The function's parameters require corresponding argument values.

    Fix: Match the number of supplied arguments to the number of parameters.

  • Confusing a parameter with an argument.

    A parameter is declared by the function; an argument is supplied by the call.

    Fix: Use parameter for the receiving name in the definition and argument for the actual value in the invocation.

  • Assuming execution continues to the next caller line before the function body runs.

    Control transfers to the function body immediately when the call is encountered.

    Fix: Trace the function body first, then continue at the line after the call when the function finishes.

  • Copying the function body for every new input.

    The same function can be called multiple times with different arguments.

    Fix: Write the behavior once in the function and invoke it wherever that behavior is needed.

Trace the Next Call

EASY

Suppose a function named describe declares two parameters, item and place. A program calls it with the arguments notebook and desk. Describe which value each parameter receives, what happens immediately after the call is encountered, and where execution continues after the function finishes.

Hints
  • Match arguments to parameters from left to right.
  • Remember the call, execute, return cycle.
  • The caller continues at the line after the function call.
MEDIUM

Explain why calling one parameterized function three times can be preferable to writing the same function body three times.

Hints
  • Consider code repetition.
  • Consider what happens when the function logic needs to be fixed or improved.

Essential Takeaways

  1. A function call transfers execution to the function body and returns control to the caller when the function finishes.
  2. Parameters are receiving variables declared by a function; arguments are the actual values supplied by a call.
  3. With multiple parameters, arguments match parameters in order.
  4. Every invocation starts a fresh execution of the function body.
  5. Repeated calls reuse one function definition, reducing code repetition and simplifying maintenance.

Key Takeaways

  • A function call pauses the caller, executes the function body, and then returns to the line after the call.
  • Parameters define the inputs a function can receive, while arguments provide the values for a particular call.
  • Arguments are matched to multiple parameters in order.
  • Calling one parameterized function repeatedly allows different inputs without duplicating the function's code.
  • To trace a call, follow the sequence: identify the function, map the arguments, execute the body, and return to the caller.