MCS-201 · Unit 11

Functions and File Handling in Python

Section 3 of 8

Block 3 · Unit 11Functions and File Handling in Python10 min · Section 3 of 8

11.3Function Arguments

Covers IGNOU MCS-201 §11.4

Python offers several flexible ways to pass arguments. Choosing well makes functions both easy to call and hard to call incorrectly.

Positional (required) arguments

Arguments are matched to parameters by position. The number must match exactly: too few or too many raises TypeError. Wrong order gives a wrong result without any error.

Default arguments

A parameter can have a default value, used when the call omits it: def simple_interest(p, r=2, t=5):. Parameters with defaults must come after those without.

Keyword arguments

The caller names the parameter: compound_interest(r=13, p=100000, t=5). Order no longer matters, and calls become self-explanatory. In a call, positional arguments must come before keyword arguments.

Arbitrary arguments

  • *args collects any number of extra positional arguments into a tuple.
  • **kwargs collects any number of extra keyword arguments into a dictionary.

The names args and kwargs are conventions; the * and ** are what matter. The order in a definition is: normal parameters, *args, then **kwargs.

How arguments are passed

Python passes a reference to the object (sometimes called “call by object reference” or “call by sharing”). If the function changes a mutable object (for example appends to a list), the caller sees the change; if it rebinds the parameter (lst = [...]), the caller does not.

The mutable default trap

A default value is created once, when the function is defined, not on every call. A mutable default such as [] is therefore shared between calls.

Key takeaways

  • Positional arguments match by order and count.
  • Defaults make parameters optional; keywords match by name.
  • *args is a tuple of extra positional values; **kwargs a dictionary of extra named values.
  • Defaults are evaluated once; use None instead of a mutable default.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does simple_interest(1000, 5) return for def simple_interest(p, r=2, t=5): return p * r * t / 100?

Show answer

Answer: B. 250.0

p = 1000, r = 5 (given), t = 5 (default): 1000 × 5 × 5 / 100 = 250.0.

Question 2 · multiple choice

Which definition is valid?

Show answer

Answer: C. def f(a, b=2, *rest, **opts):

Normal parameters, then defaults, then *args, then **kwargs. A default before a non-default parameter, ** before *, and duplicate names are all errors.

Question 3 · multiple choice

What does this program print?

Python · quiz_args.py
def show(*a, **k):
    print(len(a), sorted(k))

show(1, 2, 3, x=4, y=5)
Show answer

Answer: B. 3 [‘x’, ‘y’]

The three positional values go to the tuple a; the named values go to the dictionary k, whose sorted keys are x and y.

Question 4 · multiple choice

What is the type of args inside def f(*args):?

Show answer

Answer: D. tuple

Extra positional arguments are packed into a tuple.

Question 5 · multiple choice

What does this program print?

Python · quiz_kw.py
def power(base, exp=2):
    return base ** exp

print(power(exp=3, base=2), power(5))
Show answer

Answer: A. 8 25

Keyword arguments can be in any order: 2³ = 8; power(5) uses the default exponent 2, giving 25.

Question 6 · short answer

Write a function unique(numbers) that returns a list of the distinct items of a list in their original order (Check Your Progress 2, Ex 2), and give its output for [1, 2, 1, 1, 2, 2, 3, 4, 6, 3].

Show worked answer

def unique(numbers): out = [], then for n in numbers: if n not in out: out.append(n), then return out. For the given list it returns [1, 2, 3, 4, 6]. Each item is added only the first time it is seen, which keeps the original order. (list(dict.fromkeys(numbers)) gives the same result in one line; list(set(numbers)) removes duplicates but does not guarantee the order.)

↑ ↓ move↵ openTab switch type