↩️ 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.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

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) returns True for an even number and False for 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

  1. What does return do?

  2. If def double(n): return n * 2 and x = double(5), what is x?

  3. In Python, what does a function without return give back?

🔎 Ask Thinkapedia another question