Page 1 of 1

Pointer on Function

Posted: Thu May 20, 2004 6:47 pm
by Nico
It is possible to use Pointer on Function with Pure Basic?

:)

Posted: Thu May 20, 2004 7:09 pm
by fweil
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.

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)

Watch the results and how argument is passed and result returned, and what the effect is on the argument variable.

Rgrds

Re: Pointer on Function

Posted: Thu May 20, 2004 7:36 pm
by tinman

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)

Posted: Thu May 20, 2004 7:44 pm
by Nico
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);
}

Posted: Fri May 21, 2004 1:33 am
by Kale

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)
:D

...

Posted: Fri May 21, 2004 2:02 am
by NoahPhense
Nice example..

This needs to make it into the codearchiv ..

- np

Posted: Fri May 21, 2004 11:43 pm
by Nico
Thanks Kale :D