Concepts / Calling Built-in Functions

Calling Built-in Functions

Arguments belong to the call. Parameters belong to the function's definition and local execution.

  • Programming

From Call to Execution

When you call a function, you provide values for the function to use. Those values are arguments. The function receives them through parameters, which are variables inside the function. Understanding this handoff lets you trace what a function is working with before you consider what it does next.

Key point

Arguments belong to the call. Parameters belong to the function's definition and local execution.

Position Controls the Match

Arguments are assigned to parameters by position. The first argument goes to the first parameter, the second argument goes to the second parameter, and so on. The names do not change this positional order. To trace a call, line up the values from left to right with the parameters from left to right.

redfirst argumentfirstfirst parameterbluesecond argumentsecondsecond parameterposition 1position 2
Which argument is assigned to each parameter when the function is called?
Worked example

Tracing Two Values

Suppose a function has parameters named first and second, and it is called with the values red and blue in that order. Which value does each parameter receive?

  1. 1
    Match position one

    The first argument is red. The first parameter, first, receives red.

  2. 2
    Match position two

    The second argument is blue. The second parameter, second, receives blue.

  3. 3
    Inspect the function body

    The function body uses the parameter names first and second to refer to the values received during this call.

Result

first refers to red, and second refers to blue during this function execution.

Inside the Local Scope

After the call supplies its arguments, the function works with the corresponding parameter variables. Parameter names are local to the function, so they exist while that function is executing. The important tracing boundary is this: a value written at the call becomes available through a parameter name inside the function.

function callarguments suppliedposition matchfirst with first, secondwith secondlocal scopeparameters receive valuesfunction bodyuses parameter namespasses valuesassigns by positionmakes values available
How does a value move from a function call into the function's local scope?

Applying the Same Rule

Worked example

Predicting the Values Used

Imagine a function with parameters named item and quantity. A call supplies notebook first and three second. What values are available through the parameters while the function executes?

  1. 1
    Read the parameter order

    The parameters are ordered as item first and quantity second.

  2. 2
    Read the argument order

    The arguments are ordered as notebook first and three second.

  3. 3
    Map the positions

    Notebook is assigned to item because both are first. Three is assigned to quantity because both are second.

  4. 4
    Predict the body inputs

    Any operation in the function body that uses item receives notebook, and any operation that uses quantity receives three.

Result

During this execution, item refers to notebook and quantity refers to three.

This positional rule applies to built-in functions as well as user-defined functions. The source examples identify built-in functions such as math.sin and math.pow as functions that require arguments. The same argument-and-parameter relationship is used when a function created by a programmer receives values.

Best practice

When predicting a function's result, first write the parameters in order, then write the arguments in order beneath them. Only after making those matches should you trace what the function does with the received values.

Mistakes in Argument Mapping

  • Common mistake

    Treating the argument and parameter as the same thing

    What it looks like

    Calling a function with notebook and describing item as if it were the value written in the call.

    Why it fails

    The argument is the value passed during the call, while item is the parameter variable inside the function.

    Do this instead

    Say that notebook is the argument and item receives it as a parameter.

  • Common mistake

    Matching by meaning instead of position

    What it looks like

    Assuming the second argument should go to the parameter whose name seems most related to it.

    Why it fails

    The stated rule assigns arguments by position: first to first, second to second, and so on.

    Do this instead

    Line up the argument and parameter lists from left to right.

  • Common mistake

    Assuming parameter names exist everywhere

    What it looks like

    Referring to a function parameter as though it remains available after that function finishes executing.

    Why it fails

    Parameter names are local to the function and exist while the function is executing.

    Do this instead

    Treat the parameter as part of the function's local scope.

Mapping Practice

EASY

A function has three parameters in this order: source, destination, and count. It is called with the values folder_a, folder_b, and four in that order. Identify the argument assigned to each parameter, then state which names are local to the function while it executes.

Hints
  • Write the parameters in positions one, two, and three.
  • Place the arguments beneath them in the same order.
  • The parameter names are local to the function during its execution.
Worked example

Practice Check

Using the call described above, determine the values received by source, destination, and count.

  1. 1
    First position

    source receives folder_a.

  2. 2
    Second position

    destination receives folder_b.

  3. 3
    Third position

    count receives four.

Result

The local parameter mapping is source to folder_a, destination to folder_b, and count to four.

Key Takeaways

  1. 1
    Arguments are values supplied during a function call.
  2. 2
    Parameters are variables inside the function that receive those arguments.
  3. 3
    Arguments map to parameters by position, from first to first and onward.
  4. 4
    Parameter names are local to the function while it is executing.
  5. 5
    Built-in functions and user-defined functions use this same argument-to-parameter relationship.

At a glance

  • An argument is a value passed in a call; a parameter is the receiving variable inside the function.
  • The first argument maps to the first parameter, the second maps to the second, and so on.
  • Parameter names belong to the function's local scope while it executes.
  • Trace a call by aligning arguments and parameters by position before predicting what the function does.

Related concepts

More concepts starting with C