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
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

Edited by Scott_S
Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.