Concepts / Function Arguments and Return Values

Function Arguments and Return Values

The caller’s variable and the function’s parameter may refer to the same passed value during the call, but their names belong to different scopes. The function uses the parameter name, not the caller’s name.

  • Programming

The Naming Assumption

A common beginner assumption is that a variable passed to a function must have the same name as the function’s parameter. That assumption is incorrect. A function receives the value supplied by the caller, then refers to that value through its own parameter name. The caller’s variable name and the parameter name are independent.

Value Flow Across Scopes

Imagine a caller holding the value hello in a variable named michael. When the caller supplies michael to a function whose parameter is named bruce, the value hello becomes available inside the function through bruce. Inside the function, bruce is the meaningful name. The name michael belongs to the caller’s scope and has no meaning inside the function.

michaelcaller variablehellovaluebrucefunction parameterholdsassigned to
How does a value move into a function under a different parameter name?
Worked example

Tracing One Function Call

A caller has a variable named michael containing hello. The function has a parameter named bruce. What name does the function use for the received value?

  1. 1
    Caller state

    The caller’s variable michael holds the value hello.

  2. 2
    Value transfer

    The function receives the value held by michael.

  3. 3
    Local reference

    Inside the function, the received value is referred to by the parameter name bruce.

  4. 4
    Scope separation

    The name michael remains associated with the caller’s scope, while bruce exists in the function’s local scope.

Result

Inside the function, use bruce to refer to the received value. The function does not use michael as its parameter name.

Local Parameter Names

A parameter is created in the function’s local scope when the function is called. Its name is meaningful within that function body. The parameter cannot be accessed from outside the function or from inside another function. When the function finishes executing, that parameter ceases to exist. The caller’s variable remains in the caller’s scope.

Caller scopemichaelcaller variableFunction scopebruceparametercontainscontains
Which name belongs to each scope, and where can the parameter be used?
Key point

The caller’s variable and the function’s parameter may refer to the same passed value during the call, but their names belong to different scopes. The function uses the parameter name, not the caller’s name.

Independent Calls

The same function can be called with variables that have different names. The function’s definition does not need to change for each caller. What matters is that a value is supplied. Once the call begins, the function refers to that value through its own parameter name.

michaelcaller variablebrucesame parametersarahcaller variablebrucesame parametervalue entersvalue enters
What changes between calls, and what stays the same when different variables are passed to one function?
In the real world

Suppose one call supplies a value from a variable named michael and another call supplies a value from a variable named sarah. If the function’s parameter is bruce, the function uses bruce in both calls. The caller names may differ, but the function’s local parameter name remains the same.

Best practice

Choose parameter names that make sense inside the function, and choose caller variable names that make sense where the call occurs. Do not rename either one merely to force a match. Naming independence allows functions to remain reusable and avoids naming conflicts.

Mistakes to Avoid

  • Common mistake

    Assuming the caller’s variable name must match the parameter name.

    What it looks like

    Believing that a parameter named bruce can receive a value only from a caller variable also named bruce.

    Why it fails

    The function receives the value, not the caller’s variable name.

    Do this instead

    Use any caller variable name. Inside the function, refer to the received value through the function’s parameter name.

  • Common mistake

    Trying to use the parameter name outside its function.

    What it looks like

    Expecting bruce to be available in the caller after the function finishes.

    Why it fails

    The parameter exists only in the function’s local scope.

    Do this instead

    Use the caller’s own variable in the caller’s scope. The parameter belongs only to the function.

  • Common mistake

    Expecting the caller’s name to remain meaningful inside the function.

    What it looks like

    Using michael inside the function simply because michael was the variable supplied by the caller.

    Why it fails

    Inside the function, the value is referred to through the parameter name.

    Do this instead

    Use the parameter defined by the function when referring to the received value.

Practice Check

EASY

A function has a parameter named package. One caller passes a value held by a variable named delivery, and another caller passes a value held by a variable named message. Which name should the function use when referring to the received value?

Hints
  • Separate the caller’s scope from the function’s local scope.
  • The function uses its parameter name after the value is received.

What do you think happens?

Which name does the function use for the received value?

  • delivery

  • message

  • package

Reveal answer
Answer:

package

The caller’s variable names are independent of the parameter name. Inside the function, the value is referred to through package.

Reusable Function Calls

  1. 1
    A function receives a value from the caller, not the caller’s variable name.
  2. 2
    The caller’s variable name and the function’s parameter name are independent.
  3. 3
    Inside the function, use the parameter name to refer to the received value.
  4. 4
    Parameters exist only in the function’s local scope and cannot be accessed outside that function.
  5. 5
    Different callers can use differently named variables with the same function.

Key Takeaways

  • Argument names and parameter names do not need to match.
  • A function receives the argument’s value and refers to it through its own parameter name.
  • Parameters belong to the function’s local scope and disappear when the function finishes.
  • Naming independence makes functions more flexible and reusable.

Related concepts

More concepts starting with F