Author Topic: Passing functions directly  (Read 309 times)

Offline JohnnyB

  • Mc. Print
  • *
  • Posts: 20
    • View Profile
Passing functions directly
« 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)

-------






Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2615
    • View Profile
Re: Passing functions directly
« Reply #1 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.

« Last Edit: 2019-May-02 by bigsofty »
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)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Passing functions directly
« Reply #2 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
 
« Last Edit: 2019-May-14 by Kitty Hello »

Offline JohnnyB

  • Mc. Print
  • *
  • Posts: 20
    • View Profile
Re: Passing functions directly
« Reply #3 on: 2019-May-09 »
Yes, that's how you do it, but why not write the function name directly as a parameter?