News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Local Variables

Started by kevin, October 06, 2009, 01:51:20 PM

Previous topic - Next topic

kevin

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

PlayBASIC Code: [Select]
   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








   Make a Global variable accessible inside a function.

PlayBASIC Code: [Select]
  // 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







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


PlayBASIC Code: [Select]
  // 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