Function pointers and arguments

Share your advanced PureBasic knowledge/code with the community.
Dave L
New User
New User
Posts: 6
Joined: Sat Aug 14, 2010 9:27 am

Function pointers and arguments

Post 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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Function pointers and arguments

Post 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)
Dave L
New User
New User
Posts: 6
Joined: Sat Aug 14, 2010 9:27 am

Re: Function pointers and arguments

Post by Dave L »

Thanks a lot, Wilbert !!!

This is what I was looking for :lol:
Post Reply