Passing functions directly

Previous topic - Next topic

JohnnyB


Right now I have to do something like this:

PROTOTYPE prType : a#

FUNCTION test: a#

LOCAL testPr As prType

testPr = test

FUNCTION prPass: testPr As prType

prPass(testPr)

Is it possible to implement direct passing of the function:

prPass(test)

-------






bigsofty

#1
You need to create a function pointer(a variable of type PROTOYPE) to pass a it as a function parameter AFAIK. You shouldn't however need to assign the PROTOYPE every time in a local var for each call.

Runnable code may be helpful to understand where you are and where you want to be TBH.

Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

#2
Uhm. Would this be OK?
Code (glbasic) Select

PROTOTYPE prType : a# // notify compiler: There is a prototype for foo(double), returns double.

LOCAL testPr AS prType // create a variable, that can hold a pointer to a function of type prType

testPr = test // set the variable's value to the address of 'test' - function and prototype MUST match!
testPr(123) // call 'test' by the pointer to it.

FUNCTION test: a#
ENDFUNCTION

JohnnyB

Yes, that's how you do it, but why not write the function name directly as a parameter?