Last time, we learned how functions can receive information through parameters. But what if we want a function to give us information back after it's done working?
Engagement Message
Imagine asking a calculator to add numbers—wouldn't you want the answer back?
Functions can return values using the return
keyword. When a function returns something, it sends that value back to wherever the function was called from. The return
keyword immediately stops the function's execution.
Engagement Message
What do you think this function gives back?
To capture what a function returns, we assign the function call to a variable. The returned value gets stored in that variable for us to use later.
Engagement Message
See how we "caught" the returned value?
Not all functions need to return values. A function that just prints a message, like our sayHello()
function, doesn't need to send anything back. These are often used for performing an action.
Engagement Message
Can you spot the difference in how these might be used?
