5. Several parameters

A function can take several parameters, separated by commas. Arguments are matched to parameters by position:

  • a and b are parameters; 2 and 3 are positional arguments.
  • The first argument fills the first parameter, the second fills the second.
  • Order matters: subtract(10, 3) is not the same as subtract(3, 10).
  • The function needs one argument per parameter, unless a parameter has a default (next lesson).
#01

Add two numbers

Define add(a, b) that returns a + b. Print the result of add(2, 3).

Print exactly:

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

#02

Order matters

Define subtract(a, b) that returns a - b. Print the result of subtract(10, 3). The first argument is the number being subtracted from.

Print exactly:

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

#03

Combine two pieces of text

Define full_name(first, last) that returns the two names joined with a space. Print full_name("Ada", "Lovelace").

Print exactly:

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

#04

Use both parameters in an expression

Define repeat(word, times) that returns word * times. Print repeat("ab", 3).

Print exactly:

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