5. Several parameters
A function can take several parameters, separated by commas. Arguments are matched to parameters by position:
aandbare parameters;2and3are positional arguments.- The first argument fills the first parameter, the second fills the second.
- Order matters:
subtract(10, 3)is not the same assubtract(3, 10). - The function needs one argument per parameter, unless a parameter has a default (next lesson).
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.
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.
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.
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.