Python 3D Academyবাংলা

Parameters and arguments

🌍 In real life. A tea-stall order isn't just "tea": it's "tea, with milk, two sugars". The recipe is the same every time, but some blanks are filled in differently for each order.

Functions have blanks like that too, called parameters.

The idea in one sentence: the names in the def brackets are parameters, the values in a call's brackets are arguments, and each call fills each parameter with its argument, in order.

Its words:

  • parameter: a name in the definition, def greet(name):
  • argument: a value in the call, greet("Ana")
  • binding: putting an argument into its parameter when the call starts

Step by step, for describe("cat", "meow"):

  1. The first argument, "cat", goes to the first parameter, animal.
  2. The second, "meow", goes to sound.
  3. The body runs with those values.
def greet(name):
    print("Hi,", name)

greet("Ana")
greet("Bo")

With several parameters, the arguments are matched in order: the first argument goes to the first parameter, and so on.

def describe(animal, sound):
    print("The", animal, "says", sound)

describe("cat", "meow")
describe("meow", "cat")
Swapping the arguments swaps what the parameters hold.

Giving too few (or too many) arguments stops the program with a TypeError:

def describe(animal, sound):
    print("The", animal, "says", sound)

describe("cat")

How Python does it. Each call gets its own fresh set of parameters: calling greet("Ana") and then greet("Bo") doesn't mix them up, because the first call's name is gone when it ends. The match is purely by position: Python doesn't look at what the values mean.

Mistakes and edge cases.

  • Too few arguments. describe("cat") gives TypeError: describe() missing 1 required positional argument: 'sound' (above).
  • Too many. describe("cat", "meow", 2) gives TypeError: describe() takes 2 positional arguments but 3 were given.
  • The wrong order. Swapped arguments give no error, just a wrong result: "The meow says cat".
  • Using a parameter outside its function. name exists only while greet is running (Chapter 15 explains why).

Quiz

  1. What does this print?

    def show(x):
        print(x * 2)
    
    show(4)
    show("ab")
  2. What does this print?

    def minus(a, b):
        print(a - b)
    
    minus(10, 3)
    minus(3, 10)
  3. In def area(w, h): and area(3, 4), what are 3 and 4?

    • Arguments
    • Parameters
    • Function names
  4. Which line has a mistake?

    def add(a, b):
        print(a + b)
    
    add(1)
  5. What does this print?

    def stars(n):
        print("*" * n)
    
    for i in range(1, 4):
        stars(i)

Coding tasks

  1. Define greet(name) that prints Hello, <name>! (for example Hello, Ana!). Then ask for a name and call greet with it.

  2. Define rectangle(w, h) that prints the area w * h. Ask for a width and a height on one line, and call rectangle with them as numbers.

  3. Define repeat(word, times) that prints word repeated times times, joined into one line (for repeat("ab", 3) it prints ababab). Ask for a word, then a number, and call it.