← Back to course
Code Crew G4 / Week 10 / Tuesday
2/6
Week 10 Β· Functions that return

Tuesday

Hand it a value
// Name it, feed it, get an answer
⏱ about 15 min

Tuesday: Hand it a value

Rocket makes a function that says Boarding at gate 4. It works great until a shuttle leaves from gate 7. "Do I need a new function for every gate?" he asks.

Raven looks at the two messages. "What do you notice? The words are the same. Only the number changes."

Nova draws a little slot in the side of a glowing box. "Would you like a hint? A function can take something in through its brackets."

Parameters and arguments

A parameter is a variable in the def line. It waits to receive a value.

The value you hand in when you call the function is called an argument.

def announce(gate):
    print("Boarding at gate", gate)

announce(4)
announce(7)
The screen shows
Boarding at gate 4
Boarding at gate 7
Callgate holds
announce(4)4
announce(7)7

More than one parameter

Use commas between parameters. The arguments line up with them in the same order.

def fuel_report(ship, units):
    print(ship, "has", units, "units")

fuel_report("Shuttle", 40)
fuel_report("Rover", 15)
The screen shows
Shuttle has 40 units
Rover has 15 units
In fuel_report("Lander", 22), ship holds
and units holds
MIXED UP?
  • Read the question.
  • Tap your answer.
What does fuel_report(40, "Shuttle") show?
def stars(n):
    print("*" * n)

stars(3)
stars(1)
stars(5)
PUT THE SCREEN LINES IN ORDER FOR THE PROGRAM ABOVE
  • ?*****
  • ?***
  • ?*
WHY THIS EXERCISEOne function, three arguments, three different results.
Try it
Write a def with one parameter, like def cheer(team):.
Write three calls with different arguments.
def welcome(name, gate):
    print("Hi", name, "use gate", gate)

welcome("Kai", 3)
welcome("Ada", 5)
In welcome("Kai", 3), what does name hold?
In the second call, what does gate hold?
How many parameters does welcome have?
What is the second line on the screen?
PARAMETER CHECK
  • Read the question.
  • Tap your answer.
What does welcome(3, "Kai") show?
In def stars(n):, what is the parameter?
In stars(5), what is the argument?
stars(n) prints "*" * n. You call stars(2), then stars(4). How many stars show in all?
WHY THIS EXERCISE2 stars plus 4 stars makes 6 stars.

Your functions can take values now. Tomorrow they will give answers back.

← Monday