2. Parameters

A parameter is a name written inside the parentheses of def. It receives the argument passed by the caller. The argument's value is assigned to the parameter for that call:

  • name is a parameter; "Ada" is a positional argument.
  • Positional arguments are matched to parameters by position: the first argument goes to the first parameter.
  • The parameter is a variable that exists inside the function body.
  • Calling the function again with another argument runs the body with that new value.
#01

Pass one argument

Define greet(name) that prints Hello, Ada! when it receives "Ada". Use an f-string to put the parameter inside the sentence. Call greet("Ada").

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Call it with another argument

Define greet(name) that prints Hello, <name>! using an f-string. Call it twice: first with "Ada", then with "Alan". Each call runs the body with a different value for name.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Use the parameter in an expression

A parameter is a normal variable inside the body, so you can use it in arithmetic. Define double(n) that prints n * 2. Call double(5).

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Use the parameter with text

Define repeat(word) that prints word * 3, the text repeated three times. Call repeat("ab").

Print exactly:

Create a free account to save your progress and unlock chapter 2.