GLBasic forum

Main forum => GLBasic - en => Topic started by: JohnnyB on 2019-May-01

Title: Passing functions directly
Post by: JohnnyB on 2019-May-01

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)

-------





Title: Re: Passing functions directly
Post by: bigsofty on 2019-May-01
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.

Title: Re: Passing functions directly
Post by: Kitty Hello on 2019-May-02
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
Title: Re: Passing functions directly
Post by: JohnnyB on 2019-May-09
Yes, that's how you do it, but why not write the function name directly as a parameter?