Page 1 of 1

Function pointers and arguments

Posted: Wed Dec 28, 2011 10:20 am
by Dave L
Probably a simple question, but I do not know how to solve it:

I have this piece of code:

Code: Select all

Prototype myProto(arg1.s, arg2)
Global myFunctionPtr.myProto

Procedure test_a(arg1.s, arg2)
  Debug arg1 + " = " + Str(arg2)
EndProcedure

Procedure test_b(arg1.s, arg2)
  Debug arg1 + " ==> " + Str(arg2)
EndProcedure

myFunctionPtr = @test_a()

myFunctionPtr("ok",1)
This works fine, but I want to eliminate globals in my code: I would like to pass the reference to the function as a procedure argument.

Something like this: genericFunction( @test_a(), "ok", 1)
genericFunction() will indirectly call test_a() once implemented...

It should not be to hard to do this (Windows API uses callback functions all the time), but I simply can't figure out how.

Re: Function pointers and arguments

Posted: Wed Dec 28, 2011 10:43 am
by wilbert
Something like this ?

Code: Select all

Prototype myProto(arg1.s, arg2)

Procedure test_a(arg1.s, arg2)
  Debug arg1 + " = " + Str(arg2)
EndProcedure

Procedure test_b(arg1.s, arg2)
  Debug arg1 + " ==> " + Str(arg2)
EndProcedure

Procedure genericFunction(fn.myProto, arg1.s, arg2)
  fn(arg1, arg2)
EndProcedure

genericFunction( @test_a(), "ok", 1)

Re: Function pointers and arguments

Posted: Wed Dec 28, 2011 10:49 am
by Dave L
Thanks a lot, Wilbert !!!

This is what I was looking for :lol: