Calling procedure from variable passed to another procedure?

Just starting out? Need help? Post your questions and find answers here.
mrjiles
Enthusiast
Enthusiast
Posts: 238
Joined: Fri Aug 18, 2006 7:21 pm
Location: IL

Calling procedure from variable passed to another procedure?

Post by mrjiles »

I am trying to call a procedure from within a procedure but the one I'm trying to call is not known (a variable procedure call?). Any ideas? Maybe there's something I'm missing...


Code: Select all

Proc1("Proc2")

Procedure Proc1(WhatToCall$)
     WhatToCall$()    ; I know this won't work but it shows what I'm trying to do :-)
EndProcedure

Procedure Proc2()
     ; code....
EndProcedure

I did look through the manual but I don't know what this would be called. Thanks in advance!
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Would using CallFunctionFast(..) be useful?

Code: Select all

Procedure one()
  ;yada
EndProcedure
Procedure two()
  ;yada
EndProcedure
Procedure tre()
  ;yada
EndProcedure
Procedure etc()
  ;yada
EndProcedure

Procedure JumpAround(*par.l)
  ;yada
  CallFunctionFast(*par)
EndProcedure

JumpAround(@one())   ; Get proc addy
JumpAround(@two())

; alternatively, a jump table

jmpAddy(1)=@one   ; Get proc addy
jmpAddy(2)=@two

;etc, then

Procedure JumpAroundABit(num.l)
  ;yada
  CallFunctionFast(jmpAddy(num))
EndProcedure

JumpAroundABit(1)
JumpAroundABit(2)
Be aware though that if you pass parameters then I think that all procedures will need the same number and type of parameters.

Also that code above is eg only, may have typos.
Last edited by Dare on Tue Dec 04, 2007 2:16 am, edited 1 time in total.
Dare2 cut down to size
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Probably CallFunctionFast, based on what you've said so far. Pass the pointer to the proc using @Proc() and the receiver does a CallFunctionFast with that as parameter.

[Edit] Hey! You.. You.. Usurper!!
Last edited by netmaestro on Tue Dec 04, 2007 2:17 am, edited 1 time in total.
BERESHEIT
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

:lol:

There is a first! And only! And probably last ...


Edit - and there are typos in above, so beware. Spotted and edited some, spotted others .... not fixed
Dare2 cut down to size
PurePWNRER
User
User
Posts: 45
Joined: Mon Mar 27, 2006 5:44 am

Re: Calling procedure from variable passed to another proced

Post by PurePWNRER »

mrjiles wrote:I am trying to call a procedure from within a procedure but the one I'm trying to call is not known (a variable procedure call?). Any ideas? Maybe there's something I'm missing...


Code: Select all

Proc1("Proc2")

Procedure Proc1(WhatToCall$)
     WhatToCall$()    ; I know this won't work but it shows what I'm trying to do :-)
EndProcedure

Procedure Proc2()
     ; code....
EndProcedure

I did look through the manual but I don't know what this would be called. Thanks in advance!
First, open your window and throw blitzbasic to the street.
Secondly,

Code: Select all

Declare.l Proc1( lwPtr.l )
Declare.l Proc2()

Proc1( @Proc2() )

Procedure.l Proc1( lwPtr.l )
  If lwPtr
    CallFunctionFast(lwPtr)
  EndIf
EndProcedure

Procedure.l Proc2()
  MessageRequester("hi","hello")
EndProcedure
:/ My kingdom for a snippet!
I might bash, But I provide.
mrjiles
Enthusiast
Enthusiast
Posts: 238
Joined: Fri Aug 18, 2006 7:21 pm
Location: IL

Post by mrjiles »

Thanks to all who replied!
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Hi,

I recommend Prototypes.

Code: Select all

Prototype.l pCalling (sString.s)


Procedure.l Proc(*Call.pCalling)
  If *Call
    *Call ("Hello World")
  EndIf
EndProcedure

Procedure.l Proc2Call (sString.s)
  MessageRequester("Test" , sString)
EndProcedure 


Define *test = @Proc2Call ()


Proc (*test)
It's more professional.

Best regards

Wolf
Post Reply