Recall & Review
beginner
What does a method with a return value do?
It performs a task and then sends back a result to the place where it was called.
Click to reveal answer
beginner
How do you define a method that returns a value in Python?
Use the
def keyword to define the method, then use return followed by the value you want to send back.Click to reveal answer
beginner
What happens if a method does not have a return statement?
It returns
None by default, which means it sends back no useful value.Click to reveal answer
beginner
Why is it useful for a method to return a value?
Because it lets you use the result later in your program, like saving it in a variable or printing it.
Click to reveal answer
beginner
Example: What does this method return?<br>
def add(a, b):
return a + bIt returns the sum of
a and b. For example, add(2, 3) returns 5.Click to reveal answer
What keyword is used to send a value back from a method in Python?
✗ Incorrect
The
return keyword is used to send a value back from a method.What does a method return if there is no return statement?
✗ Incorrect
If no return statement is present, Python methods return
None by default.Which of these is a correct method that returns the square of a number?
✗ Incorrect
Only option A returns the square value properly using
return.Why might you want a method to return a value?
✗ Incorrect
Returning a value lets you reuse the result later in your program.
What will this code print?<br>
def greet():
return 'Hello!'
print(greet())✗ Incorrect
The method returns 'Hello!' which is then printed.
Explain how a method with a return value works and why it is useful.
Think about how you get answers back when you ask a question.
You got /3 concepts.
Write a simple Python method that takes two numbers and returns their product.
Use <code>def</code> and <code>return a * b</code>.
You got /3 concepts.