Computer Science Grade 7 20 min

Return Values

Return Values

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define 'return value' and explain its purpose in a function. Write a function that uses the `return` keyword to send back a single piece of data. Capture a function's return value in a variable for later use. Differentiate between a function that prints a value and a function that returns a value. Use the return value of one function as an input (argument) for another function. Trace the flow of data from a function call, through the `return` statement, and back to the main program. Have you ever used a calculator? You type in `2 + 2`, press equals, and it gives you back `4`. How does it hand that answer back to you? 🤔 In this lesson, you'll learn how to make your functions act like that calculator. We will explore how functions can...
2

Key Concepts & Vocabulary

TermDefinitionExample FunctionA named block of reusable code that performs a specific task.A function named `calculate_area` could contain the code to multiply length and width. Return ValueThe single piece of data that a function sends back to the part of the program that called it.If you have a function `add(5, 3)`, its return value would be the number `8`. Return StatementThe special keyword (like `return`) in a function that specifies which value to send back.In a function to add two numbers, the line `return sum` sends the value of the `sum` variable back. Function CallThe act of telling a function to run. The function call itself evaluates to the function's return value.The code `total = add(5, 3)` is a function call. The expression `add(5, 3)` will be replaced by its return va...
3

Core Syntax & Patterns

The `return` Keyword Syntax def function_name(parameters): # code to calculate result return result Inside a function definition, use the `return` keyword followed by the variable or value you want to send back. This is the standard way to output data from a function. The 'Exit' Rule Code inside a function stops executing once a `return` statement is reached. Think of `return` as the function's grand finale. Any code written inside the function after the `return` line will be ignored and will not run. The 'Capture' Pattern variable_name = function_name(arguments) To use a return value, you must 'catch' it. The most common way is to assign the function call to a variable. The variable will then hold the value that the functio...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You have two functions: `def calculate_subtotal(items): return items * 10` and `def calculate_final_price(subtotal): return subtotal * 1.07`. What is the final output of `print(calculate_final_price(calculate_subtotal(5)))`?
A.50
B.55
C.53.5
D.15.7
Challenging
What is the return value of `get_letter_grade(79)` for the function below? `def get_letter_grade(score):` ` if score >= 90:` ` return 'A'` ` elif score >= 80:` ` return 'B'` ` elif score >= 70:` ` return 'C'` ` else:` ` return 'F'`
A.'B'
B.'A'
C.'C'
D.'F'
Challenging
A programmer needs a function to provide a player's health value so it can be used to calculate damage. Which design is better and why? **Design A:** `def get_health(): return 100` **Design B:** `def get_health(): print(100)`
A.Design B is better because it shows the health to the user.
B.Design A is better because its return value can be stored in a variable and used in calculations.
C.Both are equally good designs.
D.Neither is a good design because they don't take any parameters.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.