Either javascript or php - what "return" means on this cases?

if ($code == UPLOAD_ERR_OK) {
return;
}

This was a php example, but I often see similar code using this return on javascript code for example.

What does it do exactly?

Thanks in advance,
Márcio

The one way around needs the entire content of the rest of the function inside the if statement (possibly hundreds of lines). The other way around reverses the if test and uses return to exit out of the function when the rest of the code doesn’t need to run so that it is no longer necessary to wrap all the rest of the code inside of an if statement.

if (!$somethingIsTrue) return;

means that there can be hundreds of statements following which will not run if the return is run.

if ($somethingIsTrue) { … }

everything else inside the function needs to be wrapped if you use this version and so it will be easy to forget that the } is supposed to match with a { that was several hundred statements earlier (or even a dozen statements earlier).

Just to complete the set on the execution path control statements, the one that hasn’t been mentioned is continue which when used inside a loop skips to the end of the current iteration of the loop but which will run the next iteration if there is one (whereas break skips all the rest of the iterations completely.

The alias for exit is die

A bit morbid perhaps, but it leaves little doubt about what it does :wink:

:nono: Don’t get it. I believe I need more info here.
I’m sorry.

I have use it often, a function with a if statement and haven’t use a return, (and btw, now that hear about it, I believe it should be a bad practice, on the cases we want to return something, not to use it. Or am I wrong?)

But looking to both functions, the only difference I can realise between those two functions is that, the second one, don’t need an else statement.
I feel I’m not getting this.

I need a push here. :smiley:

About the exit(); I thought it was “harmful”, but now that I see is relation to die, I will try to understand his place once I found one.

execution path control statements :slight_smile: Roger that! :slight_smile:

I have to try to make my first login system, but I will get back to this threat later on, thank you guys for a more complete manual reference. :slight_smile:

thank you
Márcio

Compare


function doSomething()
{
  if ($somethingIsTrue)
  {
     // do a lot of stuff, but no need to return anything
  }
}

to


function doSomething()
{
  if (!$somethingIsTrue) return;
  
  // do a lot of stuff, but no need to return anything
}

In the end they do the same thing, but in the second example I don’t need a humangous if statement to do it.

They are there because they do different things. “break” says: “end the loop, but continue executing the rest of this function” (when used in a function that is), while “return” says: “there is no point in executing the rest of this function, stop it”.

Spot on! :tup:

for “break”, see above.
exit() stops the PHP script completely, which you only do/need if something went terribly wrong and there is no way to recover.

Yes, but that’s not its only purpose. Consider the following


function plus5($num)
{
  return $num+5;
}
$b = plus5(5);

$b is 10 at the end of this little snipped, as the function plus5(5) returned 10 ($num, which was 5, plus 5 = 10).

They are used in different contexts. Return is to get out of a function, while break is used to terminate a loop.


function giveMeANumber()
{
  for ($x = 0; $x < 10; $x++)
  {
     if (sqrt($x) == 3)
     {
        return $x; // get out of this function now, returning $x
     }
     if ($x * 2 == 8)
     {
        break; // ends this "for" loop
     }
  }
  // if the loop was ended using break, the function continues here
  // if the return statement in the loop fired code below doesn't get executed
  return $x;
}

Btw, this function returns 4.
Can you see why?

I really need to learn that, no question about it. I didn’t know, or I’ve not realised like that before. That’s the first think I wanted to tell.
But, that’s not the return alone; I was asking about the usages of a return; (with anything else after it, besides the ; ). :slight_smile:

I realize that they are now used on different contexts, but the creators of javascript language could have said, for example:
Instead of using break; for loops, and return; for functions; and “something else for something else”, why not use the same for all.
Why the need to create those two? Or, for asking it differently: why should they be different? (AND I BELIEVE YOUR EXERCICE HELP ME OUT - please see below). :slight_smile:

It’s mainly a curiosity question. :slight_smile:


function giveMeANumber()
{
  for ($x = 0; $x < 10; $x++)
  {
     if (sqrt($x) == 3)
     {
        return $x; // get out of this function now, returning $x
     }
     if ($x * 2 == 8)
     {
        break; // ends this "for" loop
     }
  }
  // if the loop was ended using break, the function continues here
  // if the return statement in the loop fired code below doesn't get executed
  return $x;
}

I can try:
It will iterate from 0 to 10, starting from 0, then 1… then 2… the fact that it starts at the lowest one and ends at the highest one plays a role here.

Looking at the first if statement we can predict that:
The number with a square root of 3 is 9.
So, in order to have the condition inside the first conditional to run, we have to arrive to number 9 on our for loop.

However, we never get there, because our second if statement tell us, when you find a value of x that multiplied by 2 gives 8, (4), you return that value, (4) and exit the for loop.

The function, will then return the value of x, after that break, that is 4.

So, the break gets out of a loop, but returns nothing, it just breaks at that point. If we want to know what point it was, we can do return $x; and we will know. :slight_smile:

If all above is correct:
What about, exit(); and break; ? They seem to be used both, for stop at some time, but return nothing. One is used on switch cases, another to stop loops - but they serve the same propose no? Stop the executation at some point…

Thanks A LOT. :slight_smile:
Márcio

Thanks a lot, so we use it to, at same point, terminate the execution of a function.

Thanks a lot for the explanations. Even if not a programmer, I now recall (on Pascal lesson received 15 years ago) the difference between procedures and functions based on either they return something or not, thanks for point it out. :wink:

One last question however:
What is then, the difference between using break; and return; ?

K. Regards,
Márcio

This is usually found inside a function or method.

$variable = my_function($argument);

where my_function has something like

return FALSE; //boolean
return $data_array; // array
return 5; // int

etc.

then $variable is assigned whatever was returned.

“return;” returns null at least in PHP it does.

Yes… because despite the use of return like return something, I’m bugging with this return; alone. That I immediately ask: return “what?”.

On the example given above, I believe it makes sense to place something to return null.
However if feels weird on my newbility point of view, to have a
“if something then nothing”, so why we have a conditional if without the else?

I may think that the logic in place should be:
if something
then
nothing

if not something, (even if explicitly not there, because there is no else clause)
then
do not do “do nothing”.

Ow well… maybe I’m getting to crazy with this.

K. R.
Márcio

It doesn’t return anything. You use it in functions whose return value you’re not supposed to use. In some other languages, like Pascal, FORTRAN and some dialects of Basic, the language differentiate between subroutines (also called procedures), which have no return value, and functions, which do.

In C/C++/Java (and C#, I think) you can declare a function to have a return type of void. It is then an error to return a value, but a simple return; statement is allowed if you need to terminate the function prematurely.

You use a return statement without a value to jump out of a subroutine/prodecure. A contrived example,

function validate(fieldId) {
    var field = document.getElementById(fieldId);

    if (field != null && field.type !== "") {
        return;
    }

    throw new ValidationException(fieldId);
}

I think in JavaScript the function return value will be undefined if you exit with return;.

That is correct except for constructors where the value returned by that statement will be this instead of undefined.

JavaScript does have a void() function but all it does is to process whatever is passed to it and then return undefined.