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:
Declaring Multiple Parameters in the def Header
Widening a function to accept more inputs costs us exactly one comma:
Reading the header piece by piece:
defand the snake_case nameprint_line_totalwork exactly as before.- Inside the parentheses, we now list two parameters, separated by a comma.
quantityandpriceare 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:
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. totalis an ordinary local variable, created inside the body from the two parameters, and then reused in theprintline.
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:
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 , 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.
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 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:
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:
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:
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:
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.
Conclusion and Next Steps
Let us collect the rules of multi-parameter functions:
- Separate parameters with commas inside the
defheader. - Pass one argument per parameter, in the same order.
- If the count does not match, Python raises a
TypeErrorimmediately. - 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!
