Last time, we learned how functions can receive information through parameters. But what if we want a function to give information back to us 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.
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. Functions that just print messages or perform actions without giving back data don't need return
statements.
Engagement Message
Can you spot the difference in how these might be used?
Here's a practical example. This function calculates if someone gets a discount:
