Computer Science
Grade 5
20 min
Functions with Return Values: Getting Results
Students will learn how functions can return values (outputs) after performing calculations or operations.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a 'return value' is and why it's useful in programming.
Write a function that uses the 'return' keyword to send back a calculated result.
Call a function and store its return value in a variable for later use.
Use the return value of a function directly inside a conditional statement (like an if/else block).
Use the return value from a function as part of a mathematical calculation.
Explain the difference between a function that prints a value and a function that returns a value.
Have you ever used a calculator and wondered how it gives you the answer *back* after you press the equals button? 🤔 That's exactly what we're learning about today!
We're going to learn how to make our functions give results b...
2
Key Concepts & Vocabulary
TermDefinitionExample
FunctionA named block of code that performs a specific task, like a recipe for the computer to follow.`def say_hello():` is the start of a function named `say_hello`.
Return ValueThe single piece of information or data that a function sends back to the program after it has finished running.If a function `add(2, 3)` runs, its return value would be `5`.
return keywordThe special command in code that tells a function what value to send back. It also stops the function immediately.`return total_score` sends the value stored in the `total_score` variable back to the program.
Call a FunctionThe act of telling the program to run the code inside a function.Typing `calculate_area(10, 5)` calls the `calculate_area` function.
Store a ValueSaving the result from a function (its...
3
Core Syntax & Patterns
The 'return' Keyword Pattern
def function_name(parameters):
# code to calculate something
result = ...
return result
Inside your function, do your calculations and then use the `return` keyword followed by the variable or value you want to send back. The function stops right after the `return` statement.
Capturing the Return Value
result_variable = function_name(arguments)
To use the result from a function, you must 'catch' it in a variable. When you call the function, put a variable name and an equals sign (`=`) in front of it.
Using the Return Value Directly
if get_score() > 100:
print("High score!")
You can use a function call directly in places where you would normally use a value, like in a conditional check or a math pro...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
What is the final output of this code?
FUNCTION add(a, b)
RETURN a + b
END
FUNCTION multiply(x, y)
RETURN x * y
END
result = multiply(add(2, 3), add(1, 4))
PRINT result
A.25
B.15
C.10
D.30
Challenging
You need to create a function named `getLarger` that takes two numbers, `numA` and `numB`, and gives back the one that is bigger. Which code block does this correctly?
A.FUNCTION getLarger(numA, numB) { PRINT numA }
B.FUNCTION getLarger(numA, numB) { IF numA > numB THEN RETURN numA ELSE RETURN numB END }
C.FUNCTION getLarger(numA, numB) { IF numA > numB THEN PRINT numA ELSE PRINT numB END }
D.FUNCTION getLarger(numA, numB) { RETURN numA > numB }
Challenging
Why is it usually better for a function like `calculate_area(width, height)` to `RETURN` the area instead of printing "The area is [number]"?
A.Because `RETURN` is faster than `PRINT`.
B.Because `PRINT` can't handle numbers.
C.Because returning the number allows you to use that number for other things, like finding the cost of carpet for that area.
D.Because you are not allowed to use `PRINT` inside a function.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free