Return multiple values

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Just a quickie:

(PHP) I want to return multiple values from a function. I would think that I would need to make the values into an array or an object, probably the latter is more appropriate.

Is this right or is there an easier way?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

There are a couple of examples of this in the manual. Returning an array and using list for the assignments looks like the easiest approach.
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

i think the array will be the good one to use because it will use lesser memory.and the code will be easier to understand.i have used it in my codings and its a good thing to do.Bye take care...........
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Arrays rule!
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

Maybe this is an unintelligent post(on my part) but...I guess if you're going for conciseness...the array is probably the way to go. <u>But</u>, why is it that you can't just have a function with separate equations at the end of the funciton return the "multiple" answers like:

Code: [ Select ]

function equations(){
var x, y, z, a, b, c, d;

   x=a+b
   y=c+d
   z=x-y

   return x;
   return y;
   return z;
}
  1. function equations(){
  2. var x, y, z, a, b, c, d;
  3.    x=a+b
  4.    y=c+d
  5.    z=x-y
  6.    return x;
  7.    return y;
  8.    return z;
  9. }


I agree the array is the way to go...just wondering if this way is somehow incorrect.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

s15199d wrote:
Maybe this is an unintelligent post(on my part) but...I guess if you're going for conciseness...the array is probably the way to go. <u>But</u>, why is it that you can't just have a function with separate equations at the end of the funciton return the "multiple" answers like:

Code: [ Select ]

function equations(){
var x, y, z, a, b, c, d;

   x=a+b
   y=c+d
   z=x-y

   return x;
   return y;
   return z;
}
  1. function equations(){
  2. var x, y, z, a, b, c, d;
  3.    x=a+b
  4.    y=c+d
  5.    z=x-y
  6.    return x;
  7.    return y;
  8.    return z;
  9. }


I agree the array is the way to go...just wondering if this way is somehow incorrect.


I've always looked at it like telling a 5 year old "yes", once you tell them that you don't get a chance to say anything else. Though I could be wrong :wink:
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

s15199d wrote:
Maybe this is an unintelligent post(on my part)

not at all, carry on
Quote:
but...I guess if you're going for conciseness...the array is probably the way to go. <u>But</u>, why is it that you can't just have a function with separate equations at the end of the funciton return the "multiple" answers like:

Code: [ Select ]

function equations(){
var x, y, z, a, b, c, d;

   x=a+b
   y=c+d
   z=x-y

   return x;
   return y;
   return z;
}
  1. function equations(){
  2. var x, y, z, a, b, c, d;
  3.    x=a+b
  4.    y=c+d
  5.    z=x-y
  6.    return x;
  7.    return y;
  8.    return z;
  9. }



the reason is that when using the return of a function you would:

Code: [ Select ]
$spanglyNewVar = fancyFunction($param1, $param2);

which has no allowance for multiple values. How could you reference multiple return values if they were done separately?

Quote:
I agree the array is the way to go...just wondering if this way is somehow incorrect.
Well, now i thnik about it again, an <b>associative</b> array is probably the way to go, especially for an example like the one above.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

The return statement exits the current function, so it will end as soon as the first return is found. You can have multiple returns in a function, but as soon as the first one is found it will exit the function and not run any additional code in that function, so additional returns will never even be seen. Using the example in the manual you could write the equations function quite concisely.

PHP Code: [ Select ]
<?php
 
function equations($a, $b, $c, $d, $x, $y) {
 
 
 
     return array ($a+$b, $c+$d, $x-$y);
 
 
 
}
 
list ($result1, $result2, $result3) = equations(3,2,1,2,3,4);
 
 
 
print "result1 = $result1, result2 = $result2, result3 = $result3";
 
?>
  1. <?php
  2.  
  3. function equations($a, $b, $c, $d, $x, $y) {
  4.  
  5.  
  6.  
  7.      return array ($a+$b, $c+$d, $x-$y);
  8.  
  9.  
  10.  
  11. }
  12.  
  13. list ($result1, $result2, $result3) = equations(3,2,1,2,3,4);
  14.  
  15.  
  16.  
  17. print "result1 = $result1, result2 = $result2, result3 = $result3";
  18.  
  19. ?>


So Joebert's analogy is correct: once you return a value there's nothing more you can do from that function.
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

Ok first of all it looks to be like I'm thinking HTML/JS and y'all are thinking PHP...

B/c while still not certain...i'm pretty sure you can still use my previoiusly posted code...

Am I wrong...?

B/c the way I understand JS...is that later in a separate function I could call x,y,z and they would be equivalent to the values determined by "equations()"....correct?
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

RichB wrote:
The return statement exits the current function, so it will end as soon as the first return is found.


*penny drops and joeberts post makes sense*

Thank you Rich. I didn't know that. I assumed that it would have taken the <i>last</i> one. Why did I think that? um.... I think I just assumed that it would continue the function and later returns would override the first. Well, The only time I ever use the return keyword more than once in a function is if they are split by if's, so I've never had to worry or think about it

at s*, I don't think so. As you have declared the variables inside a function, they have <i>local scope</i>. You should not be able to reference them from outside of the function. As soon as the function ends, they are wiped from memory. Thats how it normally works, JS might be different though
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Yep, if x, y and z are declared outside of any function then they are global to the whole script in js and will retain their value after the function call, so there's no need for the return statements since you'd be assigning values to global variables (the returns won't cause an error since they're legal, but they're superfluous). However, if you were to declare x, y, z inside the function they would not be available outside it because, as rtm noted, they would be local to that function and no longer availabe after it returned..

As I understand it, the js interpreter will first look for a variable of local scope with that name to make the assignment, and if it doesn't find one, it will then look for one of global scope for the assignment. So if you declare local variables with the same name as globals they will effectively hide the globals vars and when you try to use the globals in another function you would find they were undefined (or at least unmodified). Because globals are kept in memory for the duration, and because of the potential for accidentally hiding a global by having the same name for a local, it's best to use them sparingly and name them carefully.


Quote:
The only time I ever use the return keyword more than once in a function is if they are split by if's


That's the only real practical use of multiple returns that I've come across myself. I usually do something like this in js in my own scripts:

Code: [ Select ]
function foo() {
    if(document.getElementById) {
        // do whatever with getElementById()
        return true;
    }
    return false;                                                     
}
  1. function foo() {
  2.     if(document.getElementById) {
  3.         // do whatever with getElementById()
  4.         return true;
  5.     }
  6.     return false;                                                     
  7. }


That way if it's supported I can do my thing with getElementById() and return true and the false return will never be seen, but if it isn't supported it just ignores the code in the if and returns false without generating an error. There's no need for an else, since it will either return true inside the if and end, or drop right to the last return.
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

Yea, I got y'all! It's good to know. Haven't had a need to have one function return mult. variables...but at least now I can see how/why it's done w/ arrays! Thanks for the knowledge guys!

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 56 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.