Pass initial value for a persistent variable to function.

Just starting out? Need help? Post your questions and find answers here.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Pass initial value for a persistent variable to function.

Post by Psychophanta »

As we know, we can initialize a persistent local variable 'Static' with a constant value.
Is there a way or trick to pass an initial value to one of the 'Static' variables to a function?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Pass initial value for a persistent variable to function.

Post by mk-soft »

Only with query of the static variable.
What do you want to achieve?

Code: Select all


Procedure foo(value, init = 1)
  Static count
  Protected r1
  
  If Not count
    count = init
  EndIf
  
  r1 = count
  count + 1
  ProcedureReturn r1
EndProcedure

Debug foo(0, 10)
Debug foo(0, 10)
Debug foo(0, 10)
Debug foo(0, 10)
Debug foo(0, 10)
Debug foo(0, 10)
Debug foo(0, 10)
Debug foo(0, 10)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Pass initial value for a persistent variable to function.

Post by mk-soft »

It is better to outsource the static variable and pass it as a ByRef. This allows the function to be used multiple times.

Code: Select all


Procedure foo(value, *position.integer)
  Protected r1
  
  *position\i + value
  r1 = *position\i
  ProcedureReturn r1
EndProcedure

Define pos = 10
Define pos2 = 100

Debug foo(1, @pos)
Debug foo(10, @pos2)
Debug foo(1, @pos)
Debug foo(1, @pos)
Debug foo(10, @pos2)
Debug foo(10, @pos2)
Debug foo(10, @pos2)
Debug foo(0, @pos)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Pass initial value for a persistent variable to function.

Post by Psychophanta »

Is a solution, even the variables are out of the function and things are not so organized, and you are forced to reserve variables outside for its use just inside, the variables are available for is use (read/write) outside, etc., but as workaround in a hurry could be useful.
To explain it better i would teach it like this:

Code: Select all

Procedure foo(value, *position.integer)
  *position\i + value
EndProcedure

pos = 10
pos2 = 100

foo(1, @pos):Debug pos
foo(10, @pos2):Debug pos2
foo(1, @pos):Debug pos
foo(1, @pos):Debug pos
foo(10, @pos2):Debug pos2
foo(10, @pos2):Debug pos2
foo(10, @pos2):Debug pos2
foo(0, @pos):Debug pos
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply