Jump to content

Return Values


drunkelf

Recommended Posts

Try to think of functions as being seriously EMO. They just don't communicate with the world around them without direct prompting. it's toys are it's own and sharing isn't high on the to do list.

So, to get a function to return something to the rest of the world you need to explicitly tell it to, using the return command. e.g.

$string = "nothing important";
function emoFunction(){
$toys = "now it's something important";
}
echo "\$string = $string";
$string = emoFunction();
echo "<br />after emoFunction function call \$string = $string";

if you run that little bit of code you will see that nothing important is the ruller throughout. That's because emoFunction() hasn't been told it HAS to give $string it's toys when $string asks for them.

add a return line to the function and it all changes :

$string = "nothing important";
function emoFunction(){
$toys = "now it's something important";
return $toys;
}
echo "\$string = $string";
$string = emoFunction();
echo "<br />after emoFunction function call \$string = $string";

 

Does that help? if you need/want more on functions in general let me know and I'll stick a post on my blog about them.

 

 

Disclamer : This post is in no way ment to be deroagtory against the emo population, if anything it should simply boost the self gratifying opinion that the world does indeed hate you.

Link to comment
https://forums.phpfreaks.com/topic/270686-return-values/#findComment-1392376
Share on other sites

Good question, so you understand the importance of functions however you are not seeing the value of "return".

 

I guess first, not all functions do/will return a value (in other languages you would return "void"), php doesn't use this.

 

Depending on the function you written you would potentially want to know the result of running that function. For example you were running a conditional test and wanted to know if the result is true or false, so you would return that. Maybe you are doing some math you would return the sum/difference/product/etc. Maybe you were doing some string manipulation you would return your newly formatted string.

 

Imagine if the functions built into php didn't return anything, almost all of them would be useless.

 

For example, strtolower (string to lower). Imagine if this didn't return your string in lower case, it would be rendered useless. Same goes for a whole bunch of built in functions.

 

Now whether or not your homemade functions need to return something is another story, like I said not all functions need to return a value. However, you wrote the function in the first place for a reason, most likely trying to stay DRY (Do not Repeat Yourself).

 

HTH

Link to comment
https://forums.phpfreaks.com/topic/270686-return-values/#findComment-1392393
Share on other sites

it's important to notice that return ends the execution of the function

 

Good point. A return statement "will" end the execution of a function, however you can have multiple return statements in a function. The execution of a function is terminated either by a return statement that has been called or its closing brace.

 

For example:

public function myFunction(a) {
 if (a > 10) return false;
 if(a < 0) return false;

 return a^2;
}

Link to comment
https://forums.phpfreaks.com/topic/270686-return-values/#findComment-1392415
Share on other sites

What the heck is "EMO"?

Really? You must be living under the biggest rock ever.

 

The Emos - short for Emotional - regard themselves as a cool, young sub-set of the Goths.

Although the look is similar, the point of distinction, frightening for schools and parents, is a celebration of self harm.

Link to comment
https://forums.phpfreaks.com/topic/270686-return-values/#findComment-1392541
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.