Welcome to “Return to Sender,” where we learn about the return
statement in coding. The return
statement is used in functions to send back a value, like a number or a true/false answer, called a boolean.
return
StatementLet’s look at an example. We have a function called goodBanana
that checks if a banana is good or not. It takes a parameter called band
. If the banana is rotten, the function returns false
. If the banana is yellow and good, it returns true
.
In the main code, we use an if
statement with the goodBanana
function. If the function returns true
, the code inside the if
statement runs. This means the banana is good!
Here’s a fun challenge: a monkey wants a banana, but it must be healthy. Right now, the banana’s health is 40 out of 100. We have a function called healthy
that checks if the banana’s health is 100. If it is, the function returns true
; otherwise, it returns false
.
In the main code, we wait until the banana is healthy. Once it is, the monkey can go and get the banana. Let’s see how this works!
In another challenge, the monkey needs to scare away crows before getting the banana. We have a function called save
that checks if any crows are watching. If a crow is watching, it returns "no"
. If no crows are watching, it returns "yes"
.
This shows that a function can have multiple return
statements, but only one will be executed. As soon as a return
is reached, the function stops running.
In this challenge, we use the save
function to scare away the crows. We keep scaring them until it’s safe to get the banana.
To make the code better, we notice that two if
statements have the same result. We can combine them using an or
condition. If either condition is true, we return "no"
. Otherwise, we return "yes"
.
Let’s run the improved code. It works, but we still have a challenge to get three stars by reducing the lines of code. Can you solve it?
Now it’s your turn to use the return
statement in your coding adventures. Good luck!
return
statement in coding, and why do you think it’s important?return
statement in a function might be useful? How would you apply it?return
statements affect the function’s execution? What did you learn from this?if
statements with an or
condition simplify the code? Can you provide an example from your own experience?return
statement in your coding projects, and how might you overcome them?return
statements?Imagine you are a monkey who loves bananas! Create a simple function in your favorite programming language that checks the health of a banana. Use a return
statement to send back true
if the banana’s health is 100, and false
otherwise. Test your function with different health values and see if your monkey can eat the banana!
Write a function called scareCrows
that returns "yes"
if no crows are watching and "no"
if a crow is present. Use this function in a loop to simulate scaring away crows until it’s safe for the monkey to grab the banana. Share your code with a friend and compare your solutions!
Get into pairs and role-play a function and a main code. One of you will be the function that decides if a banana is good, and the other will be the main code that acts based on the function’s return value. Use props or drawings to make it fun and interactive!
Take the existing code that checks for banana health and crow presence. Try to improve it by combining conditions using or
and reducing the number of lines. Share your improved code with the class and explain your thought process.
Think of a real-life scenario where a decision needs to be made, like checking if it’s raining before going outside. Write a function that uses a return
statement to make this decision. Present your function to the class and explain how it works.
Here’s a sanitized version of the provided YouTube transcript:
—
[Music] Welcome to “Return to Sender.” Let’s learn about the `return` statement.
`Return` is used in functions to send back a value, such as a number or a boolean. Let’s look at this code. We have a function called `goodBanana` that takes in a parameter `band`. We’re returning a boolean. In the main code, we have an `if` statement that uses the condition of the `goodBanana` function, passing in a banana. The banana gets passed into `band`, and then in the return, we determine whether it’s true or false. If the banana is rotten, we return false; if the banana is yellow, a good banana, we return true.
So, the boolean that’s returned gets passed back to where the function was called. You can think of this `goodBanana` being replaced by what’s returned. If it returns true, we have `if true`, and then we’ll run the indented code.
Here’s a challenge: the monkey wants to get the banana but must ensure it’s really healthy. Right now, it’s not so healthy; it’s 40 out of 100. Let’s look at the code. We have a function called `healthy` that does not take in a parameter. It returns the result of a comparison. We’re calling the `health` function, which returns a number and comparing it to 100. If `health` returns 100, we return true; otherwise, we return false.
In the main code, we’re going to the health zone. Since `healthy` returns a boolean (true or false), we can use it with our `until` condition. We will wait until healthy. Once we are healthy, we want to go to the banana. Let’s go ahead and run that.
[Music] Okay, looks like it worked.
[Music] Here’s another challenge where the monkey needs to get the banana but must scare away the crows first. Let’s see how we’re going to do this. In the code, we have a `save` function that does not have a parameter. We’re checking if each crow is watching. If the crow at index zero is watching, we return “no.” If the crow at index one is watching, we return “no.” If neither is watching, we return “yes.”
This introduces the concept that you can have multiple returns in a function, but only one return will ever be reached. As soon as your program reaches a return, it exits the function and does not execute the code below that return. If we reach the return “yes,” it means both if statements were false, meaning the crows were not watching.
Up to this point, we’ve used true and false as our booleans, but you can also use “yes” and “no.” So, “yes” is the same as true, and “no” is the same as false.
Now, let’s see how we’re going to use the `save` function. We want to scare those crows away. Until we are safe, we want to scare them away by saying something.
[Music] Now, what do we want to do after we’ve scared them away? We want to go and get the banana.
[Applause] Okay, let’s run that. We scared them away; that worked! Oh, we didn’t get three stars. Let’s see if we can reduce the lines of code to get three stars.
If we look at the function, we see that we have two if statements. These two if statements have slightly different conditions but the same exact indented code. Because of that, we can consolidate them into one if statement and link them with an “or.” If either of these is true, we want to return “no.” If both are true or either of them is true, we return “no”; otherwise, we return “yes.”
Let’s run that.
Okay, it worked! Oh, we still don’t have three stars, but I’m going to leave that challenge to you. Good luck! Now it’s your turn to use `return`. Good luck!
[Music]
—
This version removes any informal language, unnecessary repetitions, and maintains clarity while preserving the instructional content.
Return – In programming, “return” is a command used to send a value back from a function to the part of the program that called it. – Example sentence: The function will return the sum of the two numbers when it is called.
Function – A function is a block of code designed to perform a specific task and can be reused in a program. – Example sentence: We wrote a function to calculate the average score of the students.
Banana – In coding, “banana” is not a technical term, but it can be used as a variable name or a placeholder in examples. – Example sentence: The variable banana stores the number of times the loop has run.
True – In programming, “true” is a boolean value that represents a condition that is correct or valid. – Example sentence: The condition will evaluate to true if the user inputs the correct password.
False – In programming, “false” is a boolean value that represents a condition that is incorrect or invalid. – Example sentence: The program will continue to ask for input until the condition is no longer false.
Health – In computer games, “health” often refers to the amount of life or energy a character has. – Example sentence: The player’s health decreases each time they are hit by an enemy.
Monkey – In coding, “monkey” is not a technical term, but it can be used as a variable name or a placeholder in examples. – Example sentence: We used the variable monkey to store the number of bananas collected in the game.
Crows – In coding, “crows” is not a technical term, but it can be used as a variable name or a placeholder in examples. – Example sentence: The array crows holds the names of all the birds spotted in the park.
Code – Code refers to the instructions written in a programming language that a computer can execute. – Example sentence: We need to debug the code to find out why the program is not working correctly.
Statement – A statement is a single line of code that performs a specific action in a program. – Example sentence: The if statement checks whether the user is logged in before displaying their profile.