Main Menu

Ok another NOOB question, lol

Started by Draco9898, October 23, 2004, 11:48:21 PM

Previous topic - Next topic

Draco9898

Is there a way to DIM inside a function or Psub so that it remains in memory after the function/whatever is called?  :ph34r:
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

#1
Yes, just don't dim it again...


However, I would NOT rely upon this.



PlayBASIC Code: [Select]
Do
Cls 0
Stuff()


Sync
Loop



Function Stuff()
Static Do_I_exist
If Do_i_Exist=False
Dim Table(10)
Do_i_Exist=True
EndIf
For lp =0 To GetArrayElements(Table(),1)
Table(lp)=100*lp
Print Table(lp)
Next
EndFunction





Draco9898

You sure? Maybe I'm not sure >_< I'm talking about this:

I want to make a function to make an array and make it so It can be used by a different function?


`Function to load stuff/make arrays
Function LoadStuff()
  Dim SomeArray(20)
Endfunction

`Function to manipulate stuff/arrays
Function DoStuff()
  For x=1 to 20
     SomeArray(x)=rnd(200)
  Next X
Endfunction




If I try this then the program bites the dust because the array is gone after the first function is called?
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

#3
Yes, that certainly won't work, not just in PB, but in ANY language.   Anything created inside a function/sub is Local to the sub.   Making it invisible to the rest of your program.  


 You'll have to pass the arrays into your function(s), if your functions are to be multi purpose..  

 It's important to understand that when an array is passed, no data is moved, then Array inside the function acts like a chameleon and interfaces with the array data your passing by pointer.  As far as the function/sub is concerned it treats this array like any other.  Just that what you do to this array inside the function, affects the passed array.






PlayBASIC Code: [Select]
 Dim Stuff(20)
Dim Table(10)

; fill the STUFF array
InitMyArray(Stuff(),1000)

; fill the Table array
InitMyArray(Table(),10)

Print "Show the Contents Of the STUFF Array"
DisplayMyArray(Stuff())

Print "Show the Contents Of the TABLE Array"
DisplayMyArray(Table())

Sync
WaitKey


Function InitMyArray(ThePassedArray(),RndSize)
For lp=0 To GetArrayElements(ThePassedArray(),1)
ThePassedArray(lp)=Rnd(RndSize)
Next
EndFunction


Function DisplayMyArray(ThePassedArray())
For lp=0 To GetArrayElements(ThePassedArray(),1)
Print ThePassedArray(lp)
Next
EndFunction





    -- PlayBASIC Documentation