What Does return Do in Programming?

What Does return Mean in a Function — Explained Simply

When you’re just starting to learn programming, the phrase “a function returns a value” might sound mysterious. Like… the function secretly made something and now it’s giving it back — but where? And why?

Let me explain it in a simple way.

The return keyword gives back a value that your program creates when a function (or method) runs. This value is then sent back to where the function was called from — kind of like a boomerang.

You write the function in one place, but you might use (or “call”) it in many different places in your code. Thanks to return, the result from the function can be saved in a variable or used in another calculation.


Think of a Function Like a Kitchen Appliance

A function (called a method in some languages) is like a coffee machine.
You put in some beans (parameters), press a button (call the function), and get your coffee (the result).

def make_coffee(beans):  # this is a function
    return "Cup of coffee from " + beans

Here, return is like handing you the finished cup.
Without it, the machine made coffee, but didn’t give it to you!


If You Don’t Return the Result, You Can’t Use It

def sum(a, b):
    return a + b

def sum_and_show(a, b):
    print(a + b)
  • In the first function, you get the result — you can save it or do more with it.
  • In the second, the result is just shown on the screen — you can’t use it later.
total = sum(2, 3)         # total = 5
total2 = sum_and_show(2, 3)  # prints 5, but total2 = None

What It Looks Like in Java

int sum(int a, int b) {  // this is a method
    return a + b;
}

Here, return doesn’t just give you a number — it has to, because the method says it will return an int. Java won’t let you forget to return a value if you said you would.


And in PHP?

function sum($a, $b) {   // this is a function
    return $a + $b;
}

Same thing: return gives you the final result. You can store it like this:

$total = sum(2, 3);  // $total = 5

But if you just use echo, it’s like watching something on TV — you see it once, and it’s gone.


So Why Do We Need return?

  • Because without it, your function doesn’t give anything back — the result is lost.
  • You won’t be able to use the result in another function, or save it in a variable.

Real-Life Analogy

  • Without return: You ask a barista for coffee, they make it… and pour it down the sink.
  • With return: You ask, and they hand you the coffee — and now you can drink it, or even give it to your friend (just like using the result of a function somewhere else in your program for a different purpose).

Leave a Reply

Your email address will not be published. Required fields are marked *