Pointer on Function

Just starting out? Need help? Post your questions and find answers here.
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Pointer on Function

Post by Nico »

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

:)
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post 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
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.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Pointer on Function

Post 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)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post 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);
}
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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
--Kale

Image
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

...

Post by NoahPhense »

Nice example..

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.
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post by Nico »

Thanks Kale :D
Post Reply