Passing a procedure as a param to another procedure

Just starting out? Need help? Post your questions and find answers here.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Passing a procedure as a param to another procedure

Post by pdwyer »

Using a function pointer of some sort is it possible to pass a procedure as a parameter to another procedure?
If one of these function pointers was passed, how can I call it and pass it parameters?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Passing a procedure as a param to another procedure

Post by NicTheQuick »

You can use CallFunctionFast() which is a very low level way to call procedures and only works good with integer arguments. Or you can use Prototypes for that.

Code: Select all

Prototype.i calc(a.i, b.i)

Procedure.i add(a.i, b.i)
 	ProcedureReturn a + b
EndProcedure

Procedure.i sub(a.i, b.i)
	ProcedureReturn a - b
EndProcedure

Procedure.i execute(*proc.calc, a.i, b.i)
	ProcedureReturn *proc(a, b)
EndProcedure

Debug execute(@add(), 1, 2)
Debug execute(@sub(), 1, 2)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
STARGÅTE
Addict
Addict
Posts: 2085
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Passing a procedure as a param to another procedure

Post by STARGÅTE »

You have to define a Prototype of your procedure which you want to pass:

Code: Select all


Prototype.f Operation(Param1.f, Param2.f) ; Definition of the passed procedure

Procedure DoIt(Func.Operation, Value1.f, Value2.f) ; Just define the procedure parameter as a prototype
	
	ProcedureReturn Func(Value1, Value2)
	
EndProcedure


;- Example

Procedure.f Add(A.f, B.f)
	ProcedureReturn A+B
EndProcedure

Procedure.f Mul(A.f, B.f)
	ProcedureReturn A*B
EndProcedure

Debug DoIt(@Add(), 3, 4)
Debug DoIt(@Mul(), 3, 4)

PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Re: Passing a procedure as a param to another procedure

Post by pdwyer »

Thankyou !!! I'll look into this!

Very helpful
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Post Reply