UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on October 06, 2009, 01:51:20 PM

Title: Local Variables
Post by: kevin on October 06, 2009, 01:51:20 PM
Local Variables

  Here's some examples to help those new to this PlayBASIC programming stuff, get some idea of Variable Scope.    


  Local Variables Inside User Defined functions

[pbcode]

   Setfps 60

 Do
       Cls 0
       
      print BumpCounterFunction()
      print BumpStaticCounterFunction()

 
      Sync     
 loop



Function BumpCounterFunction()
   // When this function is entered, all variables are set to zero

   // add one to the local COUNTER variable.  This variable only exists inside this function.
   Inc Counter

   // Since Counter is local and will be initialized to zero each time we call this function
   // upon exiting Counter will always be 1

EndFunction Counter




Function BumpStaticCounterFunction()
   // Tell PB that Counter variable should retain it's value between calls
     Static Counter

   // add one to the local COUNTER variable
     Inc Counter

  // The counter variable inside this function retains it's value between calls, so
  // each time we call this function the local counter variable will be increased by 1
 
EndFunction Counter


[/pbcode]




   Make a Global variable accessible inside a function.

[pbcode]

 // Tell PB that the variable COUNTER should be visible from inside Functions & Subs
 //
 Global Counter


   Setfps 60

 Do
       Cls 0
       
      print BumpCounterFunction()
 
      Sync     
 loop



Function BumpCounterFunction()

   // add one to the GLOBAL COUNTER variable.
   Inc Counter

   // Since counter exists outside of the function it's not created/destroyed each time
   // a function is called.  So if we change it's value inside a FUNCTION or PSUB
   // the changes will be visible program wide.

EndFunction Counter

[/pbcode]




 Using LOCAL to tell PlayBASIC to ignore any global instances of a variable.


[pbcode]

 // Tell PB that the variable COUNTER should be visible from inside Functions & Subs
 Global Counter

 Setfps 60

 Do
       Cls 0
       
      print BumpCounterFunction()
      print BumpLocalCounterFunction()
 
      Sync     
 loop



Function BumpCounterFunction()

   // add one to the GLOBAL COUNTER variable.
   Inc Counter

   // Since counter exists outside of the function it's not created/destroyed each time
   // a function is called.  So if we change it's value inside a FUNCTION or PSUB
   // the changes will be visible program wide.

EndFunction Counter





Function BumpLocalCounterFunction()

   // Tell PB that inside this function you want a local variable Called COUNTER

   
      Local Counter

   // When PB sees the Counter varibale,  it first looks are any local variables
   // of that name.. If it can't find one, it looks in the Globals declarations.
   // In this case, it'll find the LOCAL copy first.  So this Counter is not the
   // global version, but the local. Which  
   
   Inc Counter

  // Since Counter is local and will be initialized to zero each time we call this function
   // upon exiting Counter will always be 1


EndFunction Counter

[/pbcode]