Concepts / Calling Functions with Multiple Arguments

Calling Functions with Multiple Arguments

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

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

Key point

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

In the real world

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.

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

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.

  1. 1
    Read the parameter order

    The first parameter is item and the second parameter is quantity.

  2. 2
    Read the argument order

    The call supplies Notebook first and 3 second.

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

Result

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.

Callerrequests the workFunction callsupplies argument valuesNamed blockruns its statementsNext program stepcan make another callrequestsrunscontinues
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

format_labelone named blockNotebook, 3first callPencil, 5second callrun withcall again with
How can the same named block be run multiple times with different argument 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.

Worked example

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.

  1. 1
    Choose the parameter

    The required target is quantity.

  2. 2
    Use its name

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

  3. 3
    Read the function's values

    Inside the function, the parameter quantity refers to the value supplied for that named parameter.

Result

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

Mistakes with Multiple Values

  • Common mistake

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

    What it looks like

    Calling format_label with Notebook and 3, then calling format_label with Pencil and 5.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Forgetting that positional arguments are matched by position.

    What it looks like

    A function's first parameter represents an item and its second parameter represents a quantity, but the supplied values are mentally read in the opposite order.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Assuming that a keyword argument is identified by its position.

    What it looks like

    A call names quantity directly but is analyzed as though only the argument's location matters.

    Why it fails

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

    Do this instead

    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.

Related concepts

More concepts starting with C