TGC Codebase Backup



Return multiple values from a function by Rob K

6th Sep 2003 11:04
Summary

Example of how to return multiple values from a function using a stack system. Stacks are useful for many other things as well.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    dim FunctionParams(10) as float
global FStackPos

DoStuff()

first=GetResult()
second=GetResult()
third=GetResult()

print first
print second
print third

do
loop

function DoStuff()
  ClearR()
  PushResult(1)
  PushResult(2)
  PushResult(3)
endfunction

function PushResult(result#)
  inc FStackPos
  FunctionParams(FStackPos) = result#
endfunction

function GetResult()
  result# = FunctionParams(FStackPos)
  dec FStackPos
endfunction result#

function ClearR()
  Empty Array FunctionParams(0)
  global dim FunctionParams(10)
  FStackPos=0
endfunction