-2

I am confused about return statement , why we need to use return in end of function , for example

 function test($a){blah;
                   blahh ;
                   blahhh;
                  blahhhhh;
                    return;}

What the use of return here? Function automatically terminates when all the statements executed , I think there is no use of return here , but this picture from http://www.w3resource.com/php/statement/return.php make me confusedenter image description here

So Can someone please explain the use of return (when we not returning any value).

beginner
  • 2,366
  • 4
  • 29
  • 53
  • 2
    The return above just breaks out of the function –  Sep 29 '16 at 08:21
  • Depending on your design and what the function is doing, you don't want to `echo` the result. – ʰᵈˑ Sep 29 '16 at 08:22
  • 1
    @ʰᵈˑ— The return statement is *after* the echo statement, so it won't prevent that. – Quentin Sep 29 '16 at 08:22
  • @Quentin I know - I'm talking about not using `echo` and just using `return`. – ʰᵈˑ Sep 29 '16 at 08:23
  • 1
    @ʰᵈˑ— What does that have to do with the question? – Quentin Sep 29 '16 at 08:23
  • @Quentin I misread the question – ʰᵈˑ Sep 29 '16 at 08:24
  • 1
    "Above picture" — Please *quote text* and don't post screen grabs of it. It would also help to link to the source. I note that it is several spelling errors and doesn't make very much sense so I'd file it under "crap tutorial" and suggest you find a better place to learn from, such as [the manual](http://php.net/return). – Quentin Sep 29 '16 at 08:28
  • Its not about echo – beginner Sep 29 '16 at 08:34
  • @Quentin Yes you are right Information in picture is confusing , , It says "Returns the argument of the function" So i thought argument of the `test` function , But its actually the argument of return function, thats why i get confused.i will take care of this next time when i post any picture from other site – beginner Sep 29 '16 at 09:05

4 Answers4

2

It depends on what you're trying to achieve.

If you write echo in several places, your code will get confusing. In general, a function that returns a value is also more versatile, since the caller can decide whether to further manipulate that value or immediately print it.

I'd recommend to stick to the convention and use return for a function.

You should check GuardClause.

Example:

function test() {
   return 10;
}

$a = test(); // $a stores the value 10

echo $a; // Prints 10
echo $a + 5; // We may want to manipulate the value returned by the function. So, it prints 15.

For further reference, check What is the difference between PHP echo and PHP return in plain English?

Community
  • 1
  • 1
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
1

In that context: You don't.

return breaks out of the function, but since it is the last statement in that function, you would break out of it anyway without the statement.

return passes its argument back to the caller, but since it doesn't have an argument, there is nothing to pass.

So it does nothing.


Don't assume that every piece of code you stumble across has a purpuse. It might be left over from an earlier version of the code where something else (which gave it meaning) has been removed. It might be written by someone cargo culting. It might be placeholder for future development.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

It's useful if you want a function to return a value.

i.e.

Function FavouritePie($who) {

     switch($who) {
                 case 'John':
                    return 'apple';

                 case 'Peter':
                    return 'Rhubarb';
                  }
}

So, considering the following:

$WhatPie = FavouritePie('John');

echo $WhatPie;

would give apple

In a really simple form, it's useful if you want a function to return something, i.e. process it and pass something back. Without a return, you'd just be performing a function with a dead end.

Some further reading: http://php.net/manual/en/function.return.php

http://php.net/manual/en/functions.returning-values.php

To add some further context specific to the answer, if the question boils down to "Do I need to add a return for the sake of it, at the end of a function, then the answer is no. But that doesn't mean the correct answer is always to leave out your return.

it depends what you want to do with $a.

If you echo $a within the function, that function will spit out $a as soon as it is called. If you return $a, assuming you set the calling of test to a variable (i.e. $something = $test('foo')), then you can use it later on.

Chris J
  • 1,441
  • 9
  • 19
1

"return" is important when you plan to call this function from other codes, it helps you to:

  1. Know if the function works correctly, or not.
  2. Obtain values from a function.
  3. Make sure other codes are not executed after return.

It might be useless when the code is simple as your sample, let's make it more complex.

function test($a){
    if(file_exists($a)){
        if(is_file($a)){
            return $a." IS A FILE\n";
        } else if(is_dir($a)) {
            return $a." IS A DIR\n";
        } else {
            return $a." EXISTS, BUT I DONT KNOW WHAT IT IS\n"
        }
    } else {
        return $a." NOT EXISTS\n";
    }
    return 0;
}


$filecheck = test("/abc/def.txt");
if($filecheck){
    echo $filecheck;
} else {
    echo "unknown error";
}

Above shows how to return a value, and do some basic handling. It is always good to implement return in your functions so you can even specify error code for your functions for reference.

Based on your example, I'll modify slightly like below:

function test($a){
    blah;
    blahh;
    blahhh;
    blahhhhh;
    return 1;
}

So I know the code is running to last line. Otherwise the function just finishes silently.

HZS
  • 167
  • 7