Concepts / Calling Built-in Functions

Calling Built-in Functions

Arguments are values passed to a function during a call; parameters are variables inside the function that receive those values.

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

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.

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

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?

Match position one: The first argument is red. The first parameter, first, receives red.

Match position two: The second argument is blue. The second parameter, second, receives blue.

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

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.

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

Applying the Same Rule

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?

Read the parameter order: The parameters are ordered as item first and quantity second.

Read the argument order: The arguments are ordered as notebook first and three second.

Map the positions: Notebook is assigned to item because both are first. Three is assigned to quantity because both are second.

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

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.

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

  • Treating the argument and parameter as the same thing

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

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

  • Matching by meaning instead of position

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

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

  • Assuming parameter names exist everywhere

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

    Fix: 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.

Practice Check

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

First position: source receives folder_a.

Second position: destination receives folder_b.

Third position: count receives four.

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

Key Takeaways

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

Key Takeaways

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