Intro to Computer Science / Unit 7: Functions
7.2 Parameters and Arguments
Notes
A parameter is a variable in the function definition. An argument is the actual value you pass when you call the function.
def greet(name):
print("Hi,", name)
greet("Ana")
greet("Ben")
- This prints
Hi, AnaandHi, Ben. - A function can have several parameters:
def area(w, h):. Arguments are matched by position. - A default value is used when no argument is given:
def power(base, exp=2):. - Calling with the wrong number of arguments causes an error.
Practice
In
def add(a, b):, what are a and b?Names in the definition are parameters; the values passed in are arguments.
Given
def show(x, y): print(x - y), what does show(10, 4) print?x is 10 and y is 4, so 10 - 4 = 6.
Given
def power(base, exp=2): print(base ** exp), what does power(5) print?exp uses its default value of 2, so 5 ** 2 = 25.
Ready for more? Take the Unit 7 test.