how to return multiple values from a function easily

Status
Not open for further replies.

shaswat

Advanced Member level 4
Joined
Jul 2, 2013
Messages
115
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Visit site
Activity points
963
I have a program in writing a code as I want to return two values simulataneosly. How can I print those value at the same. I did as usual using return but it doesn't help me. Any easy way to access two value at the same time???
 

I have needed to do the same thing. However the 'RETURN' statement will only pass one value which is local inside the function.

The only solution I could figure out was to create two global variables. Each time the function executes, it changes their values.
 

To expand on Jayanth's answer a bit (because the OP didn't say what language he's using):

As a rule of thumb, you can only return one "thing" from a function. However, most languages support lists, dictionaries, collections, arrays, user defined data structures, or some other way to store more than one value in a single "thing".
 
...and even more expansion...

If you tell the function where the results are to be stored, it can put them there before 'returning'. A single value is still returned by the function call but wherever in memory it was told to save data will be updated as well.
So if you pass addresses to the function rather than values, it will use those addresses to hold the result. The address you pass it can be of a single variable or an array of any size you choose. In the case of an array, you pass it the address of the first location in it and your code in the function can work out sucessive addresses for the other array entries.

Brian.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…