- Home /
Return multiple parameter
Hi, is it possible to pass two the different data types as a parameter and saving them on return ? Posting idea example.
int number;
bool abc;
void function(int number, bool abc); //calling function
//and when the function is returned, is it possible to save these variables, or just call the function ?
//variables number and abc are now whats returned.
public void Action(float damage,bool bleed){
..
..
return first parameter;
return second parameter;
// or not return, just assign
}
I found two posts and tried using out operator, gives couple of errors.
The out parameter `damage' must be assigned to before control leaves the current method
Hope for some ideas.
-Karl
From a pure program$$anonymous$$g perspective, if you find yourself in the situation where you need to return two different data types from a single function, then what you really need is to redesign your code (e.g. encapsulate those two data types into a single class.)
Answer by Wuzseen · Aug 14, 2013 at 04:53 AM
This is what structs and classes are for! You would return a struct or class that contains the relevant data.
Of course you could just make these values members of your class as well and assign the class members instead of returning an object.
Hi, yes Structs and classes, i don't know how i didn't though of that. Thanks.
Hmm, i tried passing or return struct data type and even variables itself,couldn't get it work, $$anonymous$$d posting a little example? I would really appreciate that.
I simply declare variables outside my functions and classes so they remain persistent through everything. This means you can change any variable in your script from in the function/class and save the return to equal a desired operation outcome or make it void.
This does have a disadvantage of constantly using more memory compared to declaring within the function/class which creates/destroys on call/exit.
You would have one class, we'll call it DataClass, in one file--and it will not extend $$anonymous$$onoBehavior:
public class DataClass {
public first parameter;
public second parameter
public DataClass(first,second) {
first parameter = first;
second parameter = second;
}
}
Then in your function simply use this as your return line:
return new DataClass(firstStuff, secondStuff);
Then you would be able to access the data like so: instance.firstParameter, instance.secondParameter
This is just pseudo code though it is a bit more C# than javascript; though the ideas work in both--you just wouldn't have the new keyword in JS.
Answer by Jamora · Aug 14, 2013 at 12:37 PM
For the sake of completeness, I must point out that in imperative programming languages you can directly modify passed parameters to effectively return more than one value as a side effect.
C# has keywords, namely *out* and *ref*, that you can use to pass the reference to the parameter, instead of a copy.
As code:
void Start(){
int aNumber = 0;
int secondNumber = 0;
bool flag = PassByReference(ref aNumber , secondNumber );
//aNumber is 5, secondNumber is 0. flag is true.
}
bool PassByReference(ref int intByReference, int copiedInt){
intByReference = 5;
copiedInt = 5;
return true;
}