Passing Multiple Arguments

Introduction: One Input Is Rarely Enough

Welcome back to Passing Data into Python Functions! In the first unit, we opened the parentheses of a def header and slipped a single parameter inside, turning a fixed greet() into a flexible greet(name). That one placeholder was enough to serve Ada, Grace, and Linus from a single definition.

Real tasks, though, rarely arrive as one lonely value. A shopping line needs a quantity and a price. A full greeting needs a first name and a last name. In this second unit, we will:

  • declare several parameters in one header;
  • pass several arguments in one call;
  • understand that position alone decides which value lands in which parameter.

Here is the output of the small program we will build together:

text
3 units at 5 each = 15
10 units at 2 each = 20

Declaring Multiple Parameters in the def Header

Widening a function to accept more inputs costs us exactly one comma:

Python
def print_line_total(quantity, price):

Reading the header piece by piece:

  • def and the snake_case name print_line_total work exactly as before.
  • Inside the parentheses, we now list two parameters, separated by a comma.
  • quantity and price are both placeholders; neither holds a value yet.
  • Both follow ordinary variable-naming rules, so descriptive snake_case names are the right choice.

This header creates a contract with anyone who calls the function: "hand me exactly two values." The same pattern extends as far as we need it; three or four parameters simply means three or four comma-separated names inside the same parentheses.

Using Both Parameters in the Body

Inside the body, each parameter behaves like a normal variable, and we may use it as many times as we like:

Python
def print_line_total(quantity, price):
    total = quantity * price
    print(quantity, "units at", price, "each =", total)

Two details deserve attention here:

  • The parameters are combined in quantity * price. This is the new capability: a result computed from several inputs at once, rather than from one.
  • total is an ordinary local variable, created inside the body from the two parameters, and then reused in the print line.

Notice also that the print call receives five comma-separated values, mixing numbers and strings freely. As we saw at the end of the previous unit, this form converts each value for display and inserts a space between them, so no str() call is needed.

Calling with Multiple Arguments: Positional Binding

The definition still prints nothing on its own. Values arrive at the call site, one argument per parameter:

Python
print_line_total(3, 5)
print_line_total(10, 2)

Let us trace the first call slowly. 3 is the first argument, so it fills the first parameter, quantity. 5 is the second argument, so it fills price. The body then runs with those two assignments in effect: total becomes 3×5=153 \times 5 = 15, and the line 3 units at 5 each = 15 is printed. The second call binds 10 to quantity and 2 to price, giving 20.

The rule behind this is short and absolute: Python matches arguments to parameters from left to right, by position.

text
          print_line_total( 3 ,  5 )
                            |    |
                            v    v
def print_line_total(quantity, price):

The Complete Script and Its Output

Putting the pieces together gives us the whole program in this course's flat demo layout: definition first, bare calls last, with no main() wrapper and no __main__ guard.

Python
# Parameters are filled positionally: first argument -> quantity, second -> price
def print_line_total(quantity, price):
    total = quantity * price
    print(quantity, "units at", price, "each =", total)


print_line_total(3, 5)
print_line_total(10, 2)

Python reads the file from top to bottom: it stores the definition, then executes the two calls in order. The body is written once but runs twice, and each run reports a different pair:

text
3 units at 5 each = 15
10 units at 2 each = 20

Getting the Count Wrong: TypeError

The header promises two parameters, and Python enforces that promise strictly. Passing the wrong number of arguments stops the program right away:

Python
print_line_total(3)        # TypeError: missing 1 required positional argument: 'price'
print_line_total(3, 5, 7)  # TypeError: takes 2 positional arguments but 3 were given

In the first line, price has nothing to bind to, so there is no way to compute total. In the second, Python has three values but only two labels to store them in. Both messages name the function and describe the mismatch, which makes this class of mistake easy to spot and repair.

There is a practical consequence worth remembering: widening a header with an extra parameter forces every existing call to be updated with an extra argument.

Getting the Order Wrong: The Silent Bug

Now consider a far more dangerous mistake. Suppose we meant 3 units at 5 each, but typed the numbers in the other order:

Python
print_line_total(5, 3)   # 5 units at 3 each = 15

No error appears. Both values are integers, so Python binds 5 to quantity and 3 to price without complaint and prints a perfectly plausible line. The total even looks right because multiplication does not care about order; the description of the purchase, however, is simply wrong.

This is the central takeaway of the unit: Python checks how many arguments we pass, never what they mean. Two habits protect us here: choose parameter names that describe their roles precisely, and read the def header before writing a call.

Beyond Numbers: Combining Two Text Parameters

Multiple parameters are not a numbers-only idea. Here is the same structure applied to text:

Python
def greet_full_name(first_name, last_name):
    full_name = first_name + " " + last_name
    print("Hello, " + full_name + "!")


greet_full_name("Ada", "Lovelace")
greet_full_name("Grace", "Hopper")

The shape matches print_line_total exactly: combine both parameters into one local value, then print it. Because both values are strings, + joins them directly with no conversion needed. The positional trap follows us here, too: calling greet_full_name("Lovelace", "Ada") raises no error at all; it simply greets a person whose name is printed backward.

text
Hello, Ada Lovelace!
Hello, Grace Hopper!

Conclusion and Next Steps

Let us collect the rules of multi-parameter functions:

  • Separate parameters with commas inside the def header.
  • Pass one argument per parameter, in the same order.
  • If the count does not match, Python raises a TypeError immediately.
  • If the order does not match, the program runs happily and gives a wrong answer.

In the practices ahead, we will read a two-parameter function and extend it with our own calls, widen it to three parameters by adding an item label, diagnose and repair a call whose numbers were swapped, and finally write a fresh two-parameter function from scratch. Then, in the next unit, we will learn how to name arguments right at the call site, which frees us from remembering the order at all. Let us open those parentheses wider and start passing pairs!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal