ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


multiple_return_values

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
multiple_return_values [2017/03/21 14:35] korshunmultiple_return_values [2019/04/19 22:41] korshun
Line 1: Line 1:
-====== Multiple return values ======+====== Multiple return values convention ======
  
-ACSUtils uses a simple convention for functions returning multiple values, like [[functions:RotateVector]]:+Some ACSUtils functions return multiple values (e.g. [[math#vector_math|vector math functions]], but also some non-math functions). ACS doesn't support returning multiple values from a function.
  
-There are global variables r1 through r8. The functions write their return values to them.+ACSUtils uses a convention to fake returning multiple values from a function. The convention is as follows: there are global variables with names ''r1'' through ''r8''If a function returns multiple values, it actually returns no value and writes the first "returned" value to ''r1'', the second one to ''r2'', and so on.
  
-    RotateVector(x, y, angle); // Call the function +Here's an example, ''RotateVector'' returns X and Y:
-    int newX = r1; // Get the first returned value +
-    int newY = r2; // And the second one+
  
-<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.+<code> 
 +RotateVector(x, y, angle); 
 +int newX = r1; 
 +int newY = r2; 
 +</code> 
 + 
 +<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:
 <code> <code>
-SomeFunctionWithMultipleReturns(...);+SomeFunctionWithMultipleReturnValues(...);
 SomeOtherFunction(r1); SomeOtherFunction(r1);
 int b = r2; int b = r2;
 </code> </code>
-Because SomeOtherFunction may call functions that **overwrite r2**!!! +Because ''SomeOtherFunction'' may call functions that **overwrite ''r2''**! Instead, strictly follow this style:
- +
-Instead, strictly follow this style:+
  
 <code> <code>
-SomeFunctionWithMultipleReturns(...);+SomeFunctionWithMultipleReturnValues(...);
 int a = r1; int a = r1;
 int b = r2; int b = r2;
Line 29: Line 32:
 </code> </code>
 </note> </note>
 +
 +<note important>Another shortcoming is that ''r1''..''r8'' are declared ''raw'' in BCC to be able to accomodate any type of return value, and thus bypass [[strict typing]], even if it's enabled.
 +
 +<code>
 +ParseFixed("1.0", 10);
 +int error = r1;
 +int result = r2; // no compiler error, despite assigning a fixed to int
 +</code>
 +
 +</note>
 +
 +But overwriting argument variables immediately after calling the function is valid:
 +<code>
 +RotateVector(x, y, angle);
 +x = r1;
 +y = r2;
 +</code>
 +
 +An example of a function returning three values:
 +<code>
 +normalize3d(x, y, z);
 +int newX = r1;
 +int newY = r2;
 +int newZ = r3;
 +</code>
  
multiple_return_values.txt · Last modified: 2019/04/22 00:32 by korshun