Pointer on Function
Pointer on Function
It is possible to use Pointer on Function with Pure Basic?

Nico,
Depending on what procedure category you would like to work with it may change the way to think and code.
As a starter code sample, you have there different ways to code a simple factor function.
Watch the results and how argument is passed and result returned, and what the effect is on the argument variable.
Rgrds
Depending on what procedure category you would like to work with it may change the way to think and code.
As a starter code sample, you have there different ways to code a simple factor function.
Code: Select all
Procedure.l Factor1(n.l)
Result.l = 1
For i = n To 2 Step -1
Result = Result * i
Next
ProcedureReturn Result
EndProcedure
Procedure Factor2(*n.l)
Value.l = PeekL(*n)
Result.l = 1
For i = Value To 2 Step -1
Result = Result * i
Next
PokeL(*n, Result)
EndProcedure
Procedure Factor3(*n.l)
Value.l = PeekL(*n)
Result.l = 1
For i = Value To 2 Step -1
Result = Result * i
Next
PokeL(*n, Result)
ProcedureReturn Result
EndProcedure
CallDebugger
n = 10
Debug "Factor1 : " + Str(Factor1(n)) + " " + Str(n)
n = 10
Debug "Factor2 : " + Str(Factor2(@n)) + " " + Str(n)
n = 10
Debug "Factor3 : " + Str(Factor3(@n)) + " " + Str(n)
Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
- tinman
- PureBasic Expert

- Posts: 1102
- Joined: Sat Apr 26, 2003 4:56 pm
- Location: Level 5 of Robot Hell
- Contact:
Re: Pointer on Function
Code: Select all
Procedure.l foo(a.s)
MessageRequester("Info", a, #PB_MessageRequester_OK)
ProcedureReturn 666
EndProcedure
address.l = @foo()
result = CallFunctionFast(address, "blah")
MessageRequester("Info", Str(result), #PB_MessageRequester_OK)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
(WinXPhSP3 PB5.20b14)
I would like to spend a function in a procedure
Code: Select all
Example in c:
int resultat (int a, int b, Int (*compare)())
{
Return (compare(a, b));
}
int Max(int a, int b)
{
printf("Avec max/n");
Return((a>b) ? a:b);
}
int Min(int a, int b)
{
printf("Avec min\n");
Return((a<b) ? a:b);
}
void main(void)
{
int result;
result=resultat( 1, 2, &Max);
printf("Max de 1 et 2 = %d\n", result);
result=resultat( 1, 2, &Min);
printf("Min de 1 et 2 = %d\n", result);
}Code: Select all
;Example in PureBasic
Procedure.l resultat(a.l, b.l, *compare)
ProcedureReturn CallFunctionFast(*compare, a, b)
EndProcedure
Procedure.l Max(a.l, b.l)
Debug "Avec max"
If a > b
ProcedureReturn a
Else
ProcedureReturn b
EndIf
EndProcedure
Procedure.l Min(a.l, b.l)
Debug "Avec min"
If a < b
ProcedureReturn a
Else
ProcedureReturn b
EndIf
EndProcedure
result.l = resultat(1, 2, @Max())
Debug "Max de 1 et 2 = " + Str(result)
result.l = resultat(1, 2, @Min())
Debug "Min de 1 et 2 = " + Str(result)- NoahPhense
- Addict

- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
...
Nice example..
This needs to make it into the codearchiv ..
- np
This needs to make it into the codearchiv ..
- np
Last edited by NoahPhense on Sat May 22, 2004 1:09 am, edited 1 time in total.

