473,382 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Return 2 values from Function

196 100+
Hi, im just wondering if some how possible to return 2 values from the same function that arn't conditional, that is it always returns 2 values.

for e.g. (this is not what my function actually does)

Expand|Select|Wrap|Line Numbers
  1. function myfunction($input1, $input2){
  2. $output1 = $input1 + 1
  3. $output2 = $input2 + 2
  4.  
  5. return $output1;
  6. return $output2; // i know this won't work but is there a way to achive this
  7. }
  8.  
If that is not possible, how can i use global varibles in php, i don't seem to able use them for some reason they don't seem to be recognised within functions even if i define them before defining the functions.

Any Help with this Matter is greatly apprecaited Thanks,
May 2 '08 #1
6 1869
coolsti
310 100+
You could do this in two ways.

Firstly, you could put the two values you wish to return together as an array, and return the array as one returned value:

return array($output1,$output2)

but then the script that calls the function needs to then break the array apart to get the individual values.

The more easier way is to use the pass by variable instead of the pass by value in the function argument list. Here an example:

Expand|Select|Wrap|Line Numbers
  1. function myfnc($input1,$input2,&$output1,&$output2)
  2. {
  3.    $output1=$input1+$input2;
  4.    $output2=$input1-$input2;
  5. }
  6.  
In this case, I return nothing, all the values to be changed are in the arguments. If I call this function in my script as:

myfnc(5,4,$out1,$out2);

then after the call, $out1 will be set to 9 and $out2 will be set to 1.
May 2 '08 #2
chazzy69
196 100+
In the second method, are u saying the in the main program, that you can access the output which are changed by the function???

Thanks for the help
May 2 '08 #3
coolsti
310 100+
Yes basically. In most software languages, you can pass an argument by value or by reference. The default is usually by value, meaning that the value of the variable gets copied to a memory location when the function is called.

Passing by reference, however, does not copy the value of the argument to another memory location, but rather passes the memory location to the function. So in this way any changes made to the variable within the function is actually made to the original variable externally to the function call.

This is the basic idea. How it is really implemented in PHP is unknown to me.

Before PHP5, you would pass an argument by reference in the call to the argument, using the "&" symbol to tell PHP that it is to be passed by reference and not variable, as such:

Expand|Select|Wrap|Line Numbers
  1. function myfnc($in1,$in2,$out1,$out2) { 
  2.   ...  some code ... 
  3. }
  4.  
  5. // call to the function myfnc, where $out1 and $out2 are passed by reference
  6. myfnc($input1,$input2,&$output1,&$output2);
  7.  
But now PHP will give you warnings, that the "&" symbol should be moved to the function instead of in the call to the function, as such:

Expand|Select|Wrap|Line Numbers
  1. function myfnc($in1,$in2,&$out1,&$out2) { 
  2.   ...  some code ... 
  3. }
  4.  
  5. // call to the function myfnc, where $out1 and $out2 are passed by reference
  6. myfnc($input1,$input2,$output1,$output2);
  7.  
But the effect is the same. In the above, the external values of the first two arguments will not be changed, even if they are changed within the function, but the values of the last two arguments will be changed if they are changed within the function.
May 2 '08 #4
ronverdonk
4,258 Expert 4TB
See chapter Pass by reference in the standard PHP documentation. That will explain it all.

Ronald
May 2 '08 #5
chazzy69
196 100+
Thanks heaps that reakky helps
May 3 '08 #6
ronverdonk
4,258 Expert 4TB
You are welcome. See you around.

Ronald
May 3 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
3
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the...
5
by: Petr Bravenec | last post by:
I have found that when I use the RETURN NEXT command in recursive function, not all records are returned. The only records I can obtain from function are records from the highest level of...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
5
by: D. Shane Fowlkes | last post by:
This may be a very basic question but it's something I've never done before. I've looked at a couple of my favorite sites and books and can't find an answer either. I can write a Function to...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
80
by: xicloid | last post by:
I'm making a function that checks the input integer and returns the value if it is a prime number. If the integer is not a prime number, then the function should return nothing. Problem is, I...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
2
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.