This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
multiple_return_values [2017/03/17 17:52] – korshun | multiple_return_values [2019/04/19 22:41] – korshun | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Multiple return values ====== | + | ====== Multiple return values |
- | ACSUtils | + | Some ACSUtils functions |
- | There are global variables r1 through r8. The functions write their return | + | ACSUtils uses a convention to fake returning multiple values from a function. The convention is as follows: there are 8 global variables |
- | | + | Here's an example, '' |
- | int newX = r1; // Get the first returned value | + | |
- | int newY = r2; // And the second one | + | |
- | <note warning> | + | < |
+ | RotateVector(x, | ||
+ | int newX = r1; | ||
+ | int newY = r2; | ||
+ | </ | ||
+ | |||
+ | <note warning> | ||
+ | You should retrieve the return values **as soon as possible**, or else they may get overwritten by other functions that return multiple values. | ||
Don't do this: | Don't do this: | ||
< | < | ||
- | SomeFunctionWithMultipleReturns(...); | + | SomeFunctionWithMultipleReturnValues(...); |
SomeOtherFunction(r1); | SomeOtherFunction(r1); | ||
- | int x = r2; | + | int b = r2; |
</ | </ | ||
- | Because SomeOtherFunction may call functions that **overwrite r2**!!! | + | Because |
- | + | ||
- | Instead | + | |
< | < | ||
- | SomeFunctionWithMultipleReturns(...); | + | SomeFunctionWithMultipleReturnValues(...); |
int a = r1; | int a = r1; | ||
int b = r2; | int b = r2; | ||
- | </ | ||
SomeOtherFunction(a); | SomeOtherFunction(a); | ||
+ | </ | ||
</ | </ | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | < | ||
+ | ParseFixed(" | ||
+ | int error = r1; | ||
+ | int result = r2; // no compiler error, despite assigning a fixed to int | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | But overwriting argument variables immediately after calling the function is valid: | ||
+ | < | ||
+ | RotateVector(x, | ||
+ | x = r1; | ||
+ | y = r2; | ||
+ | </ | ||
+ | |||
+ | An example of a function returning three values: | ||
+ | < | ||
+ | normalize3d(x, | ||
+ | int newX = r1; | ||
+ | int newY = r2; | ||
+ | int newZ = r3; | ||
+ | </ | ||