UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: Draco9898 on November 02, 2005, 10:57:26 PM

Title: #Included functions don't see global Vars
Post by: Draco9898 on November 02, 2005, 10:57:26 PM
Functions in source code that come from #includes don't recognize Global varibles. Anyway to fix this? I wanted to code very modularly...Or should I put the globals in the include as well?
Title: #Included functions don't see global Vars
Post by: kevin on November 03, 2005, 08:00:47 AM
Without an Example, i'm going to have to assume you'd done something like this.

[pbcode]
      #include "MyFunctions.pba"
      Global   SomeVariableUSedInMyFunctions
[/pbcode]  

  This will not work.  Since the code is being built in a top down fashion, it'll first include and compile the entire source from the "MyFunctions.pba"  file  at the location of the #include statement, then continue on .  

  So the functions in your library are being compiled before the global variables are created. Thus any usage of the variable name in your functions will create local instances, rather than the global instance.


   eg


[pbcode]
Function BumpCounter()
   Inc Counter
EndFunction Counter

 Global Counter

Function BumpCounter2()
   Inc Counter
EndFunction Counter

Do
   Cls 0
   print BumpCounter()
  print BumpCounter2()
  SYnc
loop
[/pbcode]


As you can see there are two functions in this example.  One prior to the global declaration and one after.  When you run this code the BumpCounter() function is using out LOCAL variable called "Counter" since it hasn't previously (to that point in the code)   seen a Global declaration called "Counter"  and the BumpCounter2() function    So Bumpcounter will return 1, and BumpCounter2() will return the global counter value + 1 forever.


   PlayBASIC documentation - #INCLUDE and other Compiler Directives (http://playbasic.com/help.php?page=COMPILER.INDEX)