Functions – Return Values, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
Functions – Return Values in one minute
What students need to know
The returned value is stored in 'result'. result = 10. Functions can return booleans. n % 2 == 0 is True when n is even.
return passes a value out of the function — print only displays it. add() returns 3+5=8, which is stored in result.
Mistakes students make
- Using print instead of return: result will be None because print doesn't send back a value.
- Code after return never runs: move print before return.
- return must be indented inside the function.
- return must be lowercase: Return is not valid Python.
PREVIOUS · MODULE 25Functions – Parameters
YOU ARE HERE · MODULE 26Functions – Return Values
NEXT · MODULE 27Functions – Scope
What students will learn in this module
Every question in Module 26
Quick questions
- What does return do?APrints a valueBSends a value back to the caller
- def add(a, b):\n return a + b\nresult = add(3, 5)\nWhat is result?A8BNone
- What's the difference between return and print?Areturn sends data back, print only displaysBThey are the same
- What does a function return if there's no return statement?ANoneB0
- def double(n):\n return n * 2\nprint(double(5))\nWhat prints?A5B10
Key terms
Students type the meaning of:
returnReturn valueNonereturn vs print
Match the pairs
def double(n):
return n * 2
print('done')Match it to what it doesdef greet():
print('Hi')
result = greet()
print(result + '!')Match it to what it doesdef double(n):
return n * 2
result = double(5)Match it to what it doesdef is_even(n):
return n % 2 == 0Match it to what it doesdef double(n):
return n * 2
print(double(4))Match it to what it doesdef hi():
print('Hello')
print(hi())Match it to what it doesFill in the blanks
def add(a, b): ______ a + b result = add(3, 5)def double(n): return n * 2 print(______(5))def square(n): return n __ 2def greet(name): ______ 'Hello ' + name msg = greet('Al')def is_even(n): return n % 2 __ 0
Spot the error
Question 1
Adef add(a, b):B print(a + b)CDresult = add(3, 5)Eprint(result)
Question 2
Adef double(n):B result = n * 2C return resultD print('Done')Eprint(double(5))
Question 3
Adef add(a, b):B total = a + bCreturn totalD print('Adding...')Eprint(add(3, 5))
Question 4
Adef add(a, b):B total = a + bC Return totalD print('Adding...')Eprint(add(3, 5))
Question 5
Adef double(n):B result = n * 2C return(result)D print('Done')Eprint(double(5))
