Concepts / Function Arguments and Return Values

Function Arguments and Return Values

Parameter names and argument variable names are independent; the function receives the value, not the 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.

holdsassigned tomichaelcaller variablehellovaluebrucefunction parameter
How does a value move into a function under a different parameter name?

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?

Caller state: The caller’s variable michael holds the value hello.

Value transfer: The function receives the value held by michael.

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

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

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.

containscontainsCaller scopemichaelcaller variableFunction scopebruceparameter
Which name belongs to each scope, and where can the parameter be used?

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.

value entersvalue entersmichaelcaller variablebrucesame parametersarahcaller variablebrucesame parameter
What changes between calls, and what stays the same when different variables are passed to one function?

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.

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

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

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

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

  • Trying to use the parameter name outside its function.

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

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

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

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

    Fix: 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. A function receives a value from the caller, not the caller’s variable name.
  2. The caller’s variable name and the function’s parameter name are independent.
  3. Inside the function, use the parameter name to refer to the received value.
  4. Parameters exist only in the function’s local scope and cannot be accessed outside that function.
  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.