Concepts / Calling Functions with Multiple Arguments

Calling Functions with Multiple Arguments

Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times. This is known as calling the function. We have already used many built-in functions such as len and range .

  • Programming

A Name for Reusable Work

A function is a reusable piece of a program. It gives a name to a block of statements so that the block can be run using that name anywhere in the program and any number of times. Calling a function means using its name to run that named block.

A function definition creates the named block. A function call uses that name to run the block with specified argument values.

Generated example: Imagine a named function called format_label with parameters item and quantity. A call can supply one item value and one quantity value. The same named block can then be called again with different values instead of creating a separate block for every item.

Matching Values to Parameters

When a function has multiple parameters, a call supplies multiple argument values. In positional calling, the position of each supplied value identifies the parameter it fills. The first supplied value is used for the first parameter, the next supplied value for the next parameter, and so on. The names in the function's parameter list describe the roles that the supplied values take inside the named block.

position 1position 2Notebookfirst supplied valueitemfirst parameter3second supplied valuequantitysecond parameter
How does each supplied value connect to the corresponding parameter inside the function?

Two Values, Two Parameters

A function has parameters item and quantity. A call supplies Notebook first and 3 second. Determine which value is associated with each parameter.

Read the parameter order: The first parameter is item and the second parameter is quantity.

Read the argument order: The call supplies Notebook first and 3 second.

Match by position: Notebook is associated with item because both occupy the first position. 3 is associated with quantity because both occupy the second position.

item is associated with Notebook, and quantity is associated with 3.

From Caller to Named Block

A call begins at the part of the program that requests the work. The supplied argument values identify the data for that particular run. Control then moves to the named block, where its statements run using the associated parameter values. After that run, the surrounding program can make another call to the same named block.

requestsrunscontinuesCallerrequests the workFunction callsupplies argument valuesNamed blockruns its statementsNext program stepcan make another call
What happens when a program calls a named block with multiple argument values?

What do you think happens?

A named function is called once with one pair of argument values and then called again with a different pair. What is reused?

  • The named block of statements
  • Only the first pair of argument values
  • A different named block created automatically for the second call
Reveal answer

Answer: The named block of statements is reused.

Functions are reusable named blocks. Calling the function again runs that block another time with the specified argument values for that call.

Repeated Calls, Different Values

Generated example: Suppose format_label has parameters item and quantity. One call associates item with Notebook and quantity with 3. A later call associates item with Pencil and quantity with 5. The function name and named block stay the same, while the argument values for the individual calls differ.

Positional and Keyword Arguments

Positional arguments identify parameters by their place in the call. Keyword arguments identify parameters by naming them. Keyword arguments are useful when a function has many parameters but a call needs to specify only some of them. In that case, the parameter name, rather than its position, identifies which value is being supplied.

Naming the Target Parameter

A function has several parameters, including item and quantity. A call needs to provide a value for quantity without relying on its position.

Choose the parameter: The required target is quantity.

Use its name: A keyword argument names quantity directly, so the supplied value is associated with that parameter by keyword instead of by position.

Read the function's values: Inside the function, the parameter quantity refers to the value supplied for that named parameter.

The value is connected to quantity by its parameter name rather than by its location in the call.

Mistakes with Multiple Values

  • Treating a function call as if it creates a new function each time.

    Both calls use the same named block. The argument values differ, but the reusable function remains the same.

    Fix: Track the function name separately from the values supplied in each call.

  • Forgetting that positional arguments are matched by position.

    The position of each supplied value determines which parameter it fills when positional calling is used.

    Fix: Compare the parameter order with the argument order before tracing the function.

  • Assuming that a keyword argument is identified by its position.

    Keyword arguments use the parameter's name instead of its position.

    Fix: Find the named parameter first, then associate the supplied value with that parameter.

Practice the Trace

EASY

A reusable function has three parameters in this order: product, category, and quantity. One call supplies Book, Stationery, and 4 in that order. Identify the value associated with each parameter. Then describe what changes if the same function is called again with Pen, Stationery, and 2.

Hints
  • Write the parameter list and supplied values in two rows.
  • Match the first value to the first parameter, the second value to the second parameter, and the third value to the third parameter.
  • The named block is reused on the second call; only the argument values for that call differ.
MEDIUM

A function has many parameters, but a call needs to specify only the parameter named quantity. Explain why a keyword argument is appropriate and how it identifies the target parameter.

Hints
  • Compare naming a parameter with relying on its position.
  • Keyword arguments use the parameter name to identify the supplied value.

Key Takeaways

  • Functions are reusable named blocks of statements.
  • Calling a function runs its named block with specified argument values.
  • With positional arguments, values are associated with parameters by order.
  • Keyword arguments associate values with parameters by name rather than position.
  • The same function can be called repeatedly with different argument values.