↩️ Return Value
6th Grade 💻 Coding
A return value is the answer a function “gives back” to whoever called it, using the word `return`. You can store it in a variable or use it in a calculation.
In the function def double(n): return n * 2, the value after return is the answer. x = double(5) stores 10 in the variable x. return also ends the function right away — any lines after it won’t run.
A common mix-up: print shows something on the screen, but it doesn’t give an answer back to the program. A function that only prints “shows” the result, but you can’t keep calculating with it. In Python, a function without return gives back a special value called None, which means “nothing.”
For example
- A juice machine: oranges go in (the parameter), juice comes out (the return value).
- A function
is_even(n)returnsTruefor an even number andFalsefor an odd one, so you can put it straight into a condition. - A function that calculates an average returns a number, so then you can write: if the average is greater than 90, show “Excellent.”
📌 Remember: print shows. return gives back.
Quick quiz
What does return do?
If def double(n): return n * 2 and x = double(5), what is x?
In Python, what does a function without return give back?