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

Wednesday

Give an answer back
// Name it, feed it, get an answer
⏱ about 15 min

Wednesday: Give an answer back

Raven needs to double the fuel number for a round trip. Rocket's double function prints the answer on the screen. "Great," Raven says. "Now add 5 for safety."

Rocket stares at the screen. The answer is just sitting there. He cannot add anything to it.

"What do you notice?" Raven asks. "Printing shows a number to us. But the program cannot use it." Nova tosses a glowing ball back to Rocket. "Would you like a hint? A function can hand its answer back."

return hands a value back

return sends a value back to the line that called the function.

The call then stands for that value. You can store it, print it or do math with it.

def double(n):
    return n * 2

x = double(4)
print(x)
print(double(10) + 5)
The screen shows
8
25
printreturn
shows a value on the screenhands a value back to the program
the program cannot use it afterthe program can store it or do math with it
def double(n):
    return n * 2

print(double(double(3)))
What number does the program above show?
WHY THIS EXERCISEA returned value can go straight into another call.
def add_safety(units):
    return units + 5

def round_trip(units):
    return units * 2

trip = round_trip(20)
trip = add_safety(trip)
print(trip)
trip after round_trip(20)
What the program shows
StatementTrue or false?
return shows the value on the screen.?
A call to a function that returns can be stored in a variable.?
return n * 2 sends back twice n.?
WHY THIS EXERCISEMixing up print and return is a very common beginner bug.
Try it
Be a return function with a grown-up.
They hand you a number on paper. You hand back the number plus 10.

Practice lab: triple it

def triple(n):
    return n * 3

a = triple(2)
b = triple(a)
print(a, b)
print(triple(1) + triple(4))
What does a hold?
What does b hold?
What does the last line show?
RETURN CHECK
  • Read the question.
  • Tap your answer.
What does a call to a return function stand for?
add_two(n) returns n + 2. What is add_two(add_two(1))?
Which line hands back n plus 1?
StatementTrue or false?
return sends the value back to the call.?
You can do math with a returned value.?
print and return do the same job.?
triple(2) gives 6.?
WHY THIS EXERCISEOnly a returned value can be used again by the program.
plus_ten(n) returns n + 10. What does print(plus_ten(5) * 2) show?
WHY THIS EXERCISEThe call is worked out first, then the math.

You can make functions that answer! Tomorrow you will build the fuel budget with them.

← Tuesday