
-----------------------------------
Albrecd
Tue Oct 09, 2007 12:59 pm

Array of Functions
-----------------------------------
I'm attempting to code for an array of functions, but I'm running into trouble with the definitions.

Example:
var FunctionArray : array 1 .. 1 of fcn x () : int

fcn FunctionExample : int
    result 5
end FunctionExample

FunctionArray (1) := FunctionExample

will cause an error, because the line FunctionArray (1) := FunctionExample means that FunctionArray (1) is now the result of FunctionExample, rather than the function itself.

How can I assign a function to an array element?
   -Thanks

-----------------------------------
Tony
Tue Oct 09, 2007 1:48 pm

Re: Array of Functions
-----------------------------------
You have to reference the function, instead of calling it. It's been a while, I don't remember the exact syntax. It might have been along the lines of &FunctionExample ? I know the Turing's GUI module makes use of this feature - look up how they pass function references to the GUI elements.

-----------------------------------
Cervantes
Tue Oct 09, 2007 2:58 pm

RE:Array of Functions
-----------------------------------
No, there's no dereferencing involved -- when doing this in Turing, anyways. You've almost got it, Albrecd. Watch:


var FunctionArray : array 1 .. 1 of fcn x () : int

fcn FunctionExample () : int
    result 5
end FunctionExample

FunctionArray (1) := FunctionExample 


All I did was add the brackets in the function definition of FunctionExample. Now it matches the type of FunctionArray. Somehow, declaring a function without parens there is slightly different from declaring it with parens. It makes sense though, because now you assign FunctionExample to the first element of FunctionArray just like that. Then when you want to call it, you can do

put FunctionExample ()

or

put FunctionArray (1) ()


-----------------------------------
Albrecd
Wed Oct 10, 2007 9:16 am

Re: Array of Functions
-----------------------------------
Great!

Thanks guys.
