Sign in

Intro to Computer Science / Unit 7: Functions

7.2 Parameters and Arguments

3 practice questionsNot yet reviewed

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, Ana and Hi, 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?
Given def show(x, y): print(x - y), what does show(10, 4) print?
Given def power(base, exp=2): print(base ** exp), what does power(5) print?

Ready for more? Take the Unit 7 test.