Arguments & Parameters
Two words for the two ends of the same slot. The parameter is the name you write in the definition; the argument is the value you supply at the call. The exam asks you to tell them apart, and the reason to care is what happens when there are two of each and you get the order wrong.
1Parameter, argument
def bill(item, qty): # item and qty are PARAMETERS
print(item, 'x', qty)
bill('Pens', 3) # 'Pens' and 3 are ARGUMENTS
bill('Books', 12)Pens x 3 Books x 12
In the definition, inside the brackets after the name. It is a variable that has no value yet — a labelled empty box waiting for the call to fill it. Sometimes called the formal parameter.
At the call, inside the brackets. It is the actual value being handed over — what goes into the box. Sometimes called the actual parameter.
2Positional parameters: the order is the meaning
Python matches arguments to parameters by position. The first argument goes into the first parameter, the second into the second, and so on. Nothing about the names is consulted.
def divide(a, b):
print(a, '/', b, '=', a / b)
divide(10, 2)
divide(2, 10)10 / 2 = 5.0 2 / 10 = 0.2
The same function, the same two numbers, two different answers. Because these are matched by position, they are called positional parameters — and swapping the arguments is not an error, it is a different question.
area(breadth, length) when the function expects (length, breadth) and the answer for a rectangle is still right — multiplication does not care. Do the same with divide or power and the answer is quietly wrong. Read the definition before you call.def power(base, exponent):
return base ** exponent
print(power(2, 5))
print(power(5, 2))32 25
3The number of arguments must match
Two parameters means exactly two arguments. Python counts, and complains precisely:
def divide(a, b):
return a / b
divide(10)Traceback (most recent call last):
File "too_few.py", line 4, in <module>
divide(10)
TypeError: divide() missing 1 required positional argument: 'b'def divide(a, b):
return a / b
divide(10, 2, 3)Traceback (most recent call last):
File "too_many.py", line 4, in <module>
divide(10, 2, 3)
TypeError: divide() takes 2 positional arguments but 3 were givenBoth messages name the function and the number, which makes them among the easiest errors in Python to fix. The next lesson — default parameters — is about the one legitimate way to call a function with fewer arguments than it has parameters.
4As many as the job needs
def student(name, cls, section, roll):
print(name, '| Class', cls, section, '| Roll', roll)
student('Riya', 12, 'A', 17)Riya | Class 12 A | Roll 17
There is no limit, but there is a limit to what a reader can keep track of. Once a function needs five or six things, the order becomes hard to remember and it is usually a sign that the data belongs together in a list or a dictionary instead.
5What the function actually receives
A parameter is a fresh name inside the function. Assigning to it changes nothing outside:
def bump(n):
n = n + 1
print('inside :', n)
x = 10
bump(x)
print('outside:', x)inside : 11 outside: 10
But if the argument is a list — something that can be changed in place — then the function and the caller are holding the same list, and a change made inside is visible outside:
def add_mark(marks, m):
marks.append(m) # changing the list itself
scores = [70, 80]
add_mark(scores, 95)
print(scores)[70, 80, 95]
n = n + 1 points the local name at a new number and leaves the caller's alone. marks.append(m) reaches into the list both names refer to. This is the same mutable / immutable distinction from Class 11, met again from the other side.6Recap
Also called the formal parameter. It has no value until a call gives it one.
Also called the actual parameter. Every call fills the parameters afresh.
First argument to first parameter. Swapping them is not an error, just a different question — and often a wrong answer.
missing 1 required positional argument, or takes 2 but 3 were given. Both messages name the number.
Reassigning it changes nothing outside. Changing a list in place does, because both names hold the same list.
- 1
Write
simple_interest(p, r, t)that returns the interest, and call it for ₹20,000 at 7% for 3 years.Hint ·
return p * r * t / 100. Check you passed them in the order the definition names them. - 2
Call a two-parameter function with one argument and read the error carefully.
Hint · It names the missing parameter, which tells you which one you forgot.
- 3
Write
swap_print(a, b)that prints them in reverse order, then call it twice with the arguments the other way round.Hint · A quick way to convince yourself that position, not name, is doing the matching.
- 4
Write a function that appends a value to a list passed in, and print the list before and after.
Hint · The change is visible outside, because a list is mutable and both names refer to the same one.
In `def bill(item, qty)` called as `bill('Pens', 3)`, what is 3?
Why does divide(2, 10) give a different answer from divide(10, 2)?
What does Python say if a two-parameter function is called with one argument?