Concepts / Multiple Parameters and Arguments

Multiple Parameters and Arguments

A parameter is a variable declared in a function's parentheses to receive input.

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

CallerFunction bodyNext linecallfinish and return
What happens after a function is called, and when does control return to the caller?

Following One Invocation

Worked example

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.

  1. 1
    First call

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

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

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

  4. 4
    Second return

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

Result

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.

Call with AdagreetFunction runperson = AdaReturncaller continuesCall with LingreetFunction runperson = LinReturncaller continuesinvokefinishcontinueinvokefinish
How can one parameterized function produce different results without duplicating its function code?
Key point

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

Parameters name receiving variables; arguments supply values during calls.

Function definitionparametersFunction callargumentpersonreceiving variableAdasupplied valuedeclaressuppliesmatches during call
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.

Worked example

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.

  1. 1
    Read the definition

    The function declares two receiving variables, first and second.

  2. 2
    Match by position

    The first supplied value, 12, is matched to first. The second supplied value, 5, is matched to second.

  3. 3
    Execute the body

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

  4. 4
    Return to the caller

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

Result

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

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

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.

Callvalue AdaCallvalue LinpersonAdapersonLinsuppliessupplies
What changes inside the function when the same function is called with different argument values?
Key point

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

  • Common mistake

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

    What it looks like

    Treating the function name alone as the invocation.

    Why it fails

    A function call requires parentheses.

    Do this instead

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

  • Common mistake

    Providing the wrong number of arguments.

    What it looks like

    Calling a function with two parameters while supplying only one argument.

    Why it fails

    The function's parameters require corresponding argument values.

    Do this instead

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

  • Common mistake

    Confusing a parameter with an argument.

    What it looks like

    Calling the supplied value a parameter.

    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like

    Reading a function call as if it were skipped temporarily.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Copying the function body for every new input.

    What it looks like

    Creating several nearly identical blocks for different values.

    Why it fails

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

    Do this instead

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

At a glance

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

Related concepts

More concepts starting with M