LambdaLabTM
Computer Science · Class 12 · Functions
FunctionsParameters⏱️ 14 min read

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

parameter_vs_argument.py
def bill(item, qty):        # item and qty are PARAMETERS
    print(item, 'x', qty)

bill('Pens', 3)             # 'Pens' and 3 are ARGUMENTS
bill('Books', 12)
Output
Pens x 3
Books x 12
Parameter

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.

Argument

At the call, inside the brackets. It is the actual value being handed over — what goes into the box. Sometimes called the actual parameter.

Key Takeaway
A parameter is a name; an argument is a value. Every call fills the parameters afresh, which is why the same function printed “Pens x 3” once and “Books x 12” the next time without a single line of it changing.

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.

positional.py
def divide(a, b):
    print(a, '/', b, '=', a / b)

divide(10, 2)
divide(2, 10)
Output
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.

Watch Out
This is the bug that does not announce itself. Write 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.
power.py
def power(base, exponent):
    return base ** exponent

print(power(2, 5))
print(power(5, 2))
Output
32
25

3The number of arguments must match

Two parameters means exactly two arguments. Python counts, and complains precisely:

too_few.py
def divide(a, b):
    return a / b

divide(10)
Output
Traceback (most recent call last):
  File "too_few.py", line 4, in <module>
    divide(10)
TypeError: divide() missing 1 required positional argument: 'b'
too_many.py
def divide(a, b):
    return a / b

divide(10, 2, 3)
Output
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 given

Both 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

many_params.py
def student(name, cls, section, roll):
    print(name, '| Class', cls, section, '| Roll', roll)

student('Riya', 12, 'A', 17)
Output
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:

immutable_arg.py
def bump(n):
    n = n + 1
    print('inside :', n)

x = 10
bump(x)
print('outside:', x)
Output
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:

mutable_arg.py
def add_mark(marks, m):
    marks.append(m)          # changing the list itself

scores = [70, 80]
add_mark(scores, 95)
print(scores)
Output
[70, 80, 95]
Key Takeaway
The difference is whether you rebind the name or change the object. 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.
parameters.py

6Recap

Parameter = the name in the def

Also called the formal parameter. It has no value until a call gives it one.

Argument = the value at the call

Also called the actual parameter. Every call fills the parameters afresh.

Position decides the match

First argument to first parameter. Swapping them is not an error, just a different question — and often a wrong answer.

The count must match exactly

missing 1 required positional argument, or takes 2 but 3 were given. Both messages name the number.

A parameter is local

Reassigning it changes nothing outside. Changing a list in place does, because both names hold the same list.

✍️ Now write these yourself
  1. 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. 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. 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. 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.

Quick Check

In `def bill(item, qty)` called as `bill('Pens', 3)`, what is 3?

Quick Check

Why does divide(2, 10) give a different answer from divide(10, 2)?

Quick Check

What does Python say if a two-parameter function is called with one argument?