PDA

View Full Version : Return two values


spiderman3
03-15-2008, 04:35 AM
Hi
Is it possible to return two values from a procedure. I want to return a String and a float from procedure is it possible then how............?

Diamant
03-15-2008, 06:32 AM
you can pass an array by reference to achieve what you want.. for example


global proc testref()
{
float $num[];
string $name[];
refpass $num $name;
print $num;
print $name;
}

global proc refpass(float $num[], string $name[])
{
$num[0] = 1.0;
$num[1] = 2.0;
$name[0] = "first name";
$name[1] = "second name";
}

now you technically have 2 return types. what is happening is you are actually modifying the original values inside of another procedure. you can do this with as many variables as you want... as long as they are arrays.

-Rich

GiantG
03-15-2008, 09:39 AM
Correct me if I'm wrong, but you are just printing out and not returning values
So you can't store them into variables and that's what imho procedures are made for...

I dont' know of a way to to this without casting the float to a string, return the whole string, cut the string to pieces and convert them back to a float etc...

unsmoothed
03-15-2008, 11:27 AM
Hello,

what I've done in the past is to stick all the results I want to return in to an array... like what Diamant is getting at... for the float value, you can promote the value to a string on the fly as you stick it into the array.

Hope this short example helps explain:

global proc string[] testProc() {
string $result[];

$result[0] = string(23); // promote the float value of 23 to a string
$result[1] = "pSphere1"; // standard string in the next slot

return $result; // return the array
}

testProc();

Buexe
03-15-2008, 02:29 PM
And if you want to get really nasty you can of course use global variables, of course each proc would need to know the name of the glabal vars used. But than it is not that messy if you want to push around some strings, doubles, ints etc. But the array strategy should work fine in many cases...

Diamant
03-15-2008, 09:53 PM
GiantG- by printing i am showing that the value is being returned to the first procedure. you do not have to type cast into a string.. if you look closely at the code i've writen you will see the two variables that are created in testref passed on to refpass, modified in refpass, and then "returned" to testref.. by printing i am showing you that the values have been modified and passed back into the original procedure.

you can use global variables, but they are very dirty and not recommended. you can also type cast but that, again, is dirty and requires you to split apart the array.. doing what I showed is the easiest method.. go through the code and you will see that its the same thing as returning a value except you can technically have multiple values returned since the variables are passed by reference instead of value.

hopefully this makes sense. Its kinda a C++ mentality which is faster, more efficient, and requires less memory.

-Rich

GiantG
03-15-2008, 11:29 PM
After thinking a bit deeper on your code yes... it makes sense :thumbsup:

CGTalk Moderation
03-15-2008, 11:29 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.