PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue May 17, 2005 12:25 pm
ok, standard return
Code: Select all
// function definition
function add1 ($value)
{
$value = ($value + 1);
return $value;
}
// call and retrieval
$value = 26;
$number = add1 ($value)
what if i wanted to do...
Code: Select all
function add1 ($value)
{
$value = ($value + 1);
$multiply = ($value * 5);
return $value;
return $multiply;
}
how would i write my code for actually returning it, eg (complete pseudo code).
can it be done without seperate function calls?
Roja
Tutorials Group
Posts: 2692 Joined: Sun Jan 04, 2004 10:30 pm
Post
by Roja » Tue May 17, 2005 12:32 pm
Cheesy, but accomplishes the request:
Code: Select all
function add1 ($value)
{
$value = ($value + 1);
$multiply = ($value * 5);
$returnї1] = $value;
$returnї2] = $multiply;
return $return;
}
$return = add1($value);
$value = $returnї1];
$multiply = $returnї2];
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Tue May 17, 2005 1:08 pm
yup, I think that's the only way to do it...as soon as the function hits return, it's all over and it dies a horrible death.
putting everything you want into an array and returning the array is the only way to go...
Roja
Tutorials Group
Posts: 2692 Joined: Sun Jan 04, 2004 10:30 pm
Post
by Roja » Tue May 17, 2005 1:47 pm
Burrito wrote: yup, I think that's the only way to do it...as soon as the function hits return, it's all over and it dies a horrible death.
putting everything you want into an array and returning the array is the only way to go...
Actually, I can think of other ways to do it.. although each is more horrible than the first:
Code: Select all
function add1 ($value)
{
global $value, $multiply;
$value = ($value + 1);
$multiply = ($value * 5);
}
add1($value);
(Evil for the use of global-scope variables)
There are others, but I suspect the fill-an-array, return-the-array approach is the 'cleanest' architecturally. I'd be interested in hearing better ways.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue May 17, 2005 1:59 pm
I would too but from a function alone it seems like common sense to just use an array.
I have never really thought of trying any other way...
andre_c
Forum Contributor
Posts: 412 Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah
Post
by andre_c » Tue May 17, 2005 5:19 pm
another way... pass parmameters as references, but that does get uglier
Todd_Z
Forum Regular
Posts: 708 Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan
Post
by Todd_Z » Tue May 17, 2005 5:34 pm
Code: Select all
function doSomething ( $val ) {
$vals = array();
$vals[] = $val % 5;
$vals[] = $val % 6;
return $vals;
}
list( $ret1, $ret2 ) = doSomething( 929 );
echo "Return value #1: $ret1\t\t Return value #2: $ret2";