Page 1 of 1
Return in a function
Posted: Tue Nov 14, 2006 10:52 am
by iantimmy123
I've started looking at using functions and am a bit confused about the purpose of return().
I have looked on the net but all the examples that I understand I can see other statements that can be used more effectively, or the code they give as an example doesn't work. It may be that the code I'm looking at is too simple as some excerpts where it is used is way above my current understanding.
I'm probably just having a blonde day but if someone has a way of explaining to me in a practical sense I would much appreciate it as it's really bugging me
Thanks
Ian
Posted: Tue Nov 14, 2006 11:00 am
by aaronhall
If a function is used in the context of an expression, the return value is used in that expression. Here's a little example:
Code: Select all
<?
function squareNumber($number) {
$returnVal = $number*$number;
return $returnVal;
}
$num = 2;
$squaredNum = squareNumber($num);
// $squaredNum = 4
?>
the function squareNumber() takes $number and squares it. The $returnVal is assigned to $squaredNum automatically.
Posted: Tue Nov 14, 2006 11:10 am
by BDKR
It's actually most correct to say that whatever you are writing is a function
only if it returns something. Otherwise, it's a sub-routine.
For all that matters.....
The purpose of return is simple. Return the output of the logic in question. You may need it to compute something for your right? Don't want to duplicate that code in 835 places in your application? Put it in a function. The result is what's
returned back to you.
Something else that may be confusing you also is how the php manual may say something like....
Code: Select all
string sha1 ( string str [, bool raw_output] )
/* Or */
int crc32 ( string str )
As you can see above, the function names are sha1 and crc32 proper. So what's the 'string' and 'int' business in front of the function names? That's to tell you what variable type the functions will return. sha1() will return a string and crc32 will return an integer.
I trust you can take it from there.
Cheers,
BDKR
Posted: Tue Nov 14, 2006 11:17 am
by iantimmy123
lol right I've got it. Thanks guys
Posted: Tue Nov 14, 2006 11:41 am
by Luke
I've started looking at using functions and am a bit confused about the purpose of return().
It should also be noted that return is not a function and therefor does not require the parentheses...
Code: Select all
function returnMe($value){
return $value; // See... no parentheses
}
Posted: Tue Nov 14, 2006 12:19 pm
by BDKR
The Ninja Space Goat wrote:I've started looking at using functions and am a bit confused about the purpose of return().
It should also be noted that return is not a function and therefor does not require the parentheses...
Code: Select all
function returnMe($value){
return $value; // See... no parentheses
}
Yep!
Posted: Tue Nov 14, 2006 1:36 pm
by Chris Corbyn
it should also be noted that return isn't only used in functions. You can place it anywhere you want a value back from a call to something:
your_include.php
your_file.php
Code: Select all
<?php
$number = include("your_include.php");
echo $number * 10; //420
eval()'d code:
Code: Select all
<?php
$number = eval("return 7 * 3;");
echo $number; //21