drunkelf Posted November 14, 2012 Share Posted November 14, 2012 Maybe I am just tired but I cannot seem to grasp returning values from functions using the return keyword thingy and its practical application.Maybe you can break it down for me Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 14, 2012 Share Posted November 14, 2012 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. Quote Link to comment Share on other sites More sharing options...
Scott_S Posted November 14, 2012 Share Posted November 14, 2012 What the heck is "EMO"? Quote Link to comment Share on other sites More sharing options...
Scott_S Posted November 14, 2012 Share Posted November 14, 2012 (edited) 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 November 14, 2012 by Scott_S Quote Link to comment Share on other sites More sharing options...
Manixat Posted November 14, 2012 Share Posted November 14, 2012 it's important to notice that return ends the execution of the function Quote Link to comment Share on other sites More sharing options...
Scott_S Posted November 14, 2012 Share Posted November 14, 2012 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; } Quote Link to comment Share on other sites More sharing options...
Barand Posted November 14, 2012 Share Posted November 14, 2012 For example: public function myFunction(a) { if (a > 10) return false; if(a < 0) return false; return a^2; } You could at least have given a PHP example. Have you noticed the name of this forum. Quote Link to comment Share on other sites More sharing options...
Scott_S Posted November 14, 2012 Share Posted November 14, 2012 You could at least have given a PHP example. Have you noticed the name of this forum. That is. Oh, I see, maybe the ^ char. So that would be: return a = pow(a, 2); Quote Link to comment Share on other sites More sharing options...
Barand Posted November 14, 2012 Share Posted November 14, 2012 That is. Oh, I see, maybe the ^ char. So that would be: return a = pow(a, 2); Not with variable names like that. $a Quote Link to comment Share on other sites More sharing options...
Scott_S Posted November 14, 2012 Share Posted November 14, 2012 Not with variable names like that. $a Geez, nice, brain dump ... oops. Never the less point driven home. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted November 15, 2012 Share Posted November 15, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.