Computer Science Canada

Array of Functions

Author:  Albrecd [ Tue Oct 09, 2007 12:59 pm ]
Post subject:  Array of Functions

I'm attempting to code for an array of functions, but I'm running into trouble with the definitions.

Example:
code:
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

Author:  Tony [ Tue Oct 09, 2007 1:48 pm ]
Post subject:  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.

Author:  Cervantes [ Tue Oct 09, 2007 2:58 pm ]
Post subject:  RE:Array of Functions

No, there's no dereferencing involved -- when doing this in Turing, anyways. You've almost got it, Albrecd. Watch:

code:

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
code:

put FunctionExample ()

or
code:

put FunctionArray (1) ()

Author:  Albrecd [ Wed Oct 10, 2007 9:16 am ]
Post subject:  Re: Array of Functions

Great!

Thanks guys.


: