Loading...

Python Parameters

pour() on the last page always wrote tea. Those parentheses were empty, so the call passed nothing in. A parameter is a name you write inside the parentheses of def. One call fills that name with the value written in the call. The next call fills it again.

greet has one parameter, name. The print line is indented four spaces and writes name. The first call passes lida. The second call passes guest.

Pass a name into greet

python
def greet(name):
    print(name)

greet("lida")
greet("guest")

The screen shows lida, then guest. For the first call, name is lida. For the second call, name is guest. lida does not stay in name after that call ends.

Two names, in the order you write them

serve has two parameters. name is the first. drink is the second. The first value in the call fills name. The second value fills drink. One print writes name. The next print writes drink.

Fill name, then drink

python
def serve(name, drink):
    print(name)
    print(drink)

serve("lida", "tea")

The screen shows lida, then tea. name is lida because lida was written first. drink is tea because tea was written second.

Python does not look at the word and decide that tea is a drink. It uses the order in the call. Put the two values the other way around, and the two lines change with them.

Swap the two values

python
def serve(name, drink):
    print(name)
    print(drink)

serve("tea", "lida")

The screen shows tea, then lida. For this call, name is tea and drink is lida. The program does not stop. Your order is the order it used.

The counts have to match

greet has one parameter, so the call needs one value. greet() passes nothing, and the program stops. greet("lida", "guest") passes two values into one name, and the program stops. serve needs two values. serve("lida") stops, because drink was never filled.

These functions write the values on the screen. They do not hand a value back to the line that called them. The next page is about handing a value back.

Read the calls

  1. greet("lida"), then greet("guest"). What are the two lines?
  2. serve("lida", "tea"). What is the first line?
  3. serve("tea", "lida"). What is the first line now?
  4. You write greet() with nothing inside the parentheses. Does the program finish?

One reading

A parameter is a name in the parentheses of def. The call fills it. greet("lida") then greet("guest") writes lida, then guest, and the first value does not remain. serve("lida", "tea") writes lida, then tea. serve("tea", "lida") writes tea, then lida. If the number of values does not match the number of names, the program stops.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top