Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: visibility of variables

  1. #11
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    I don't think so (PBCC6).

    The code, as shown below, compiles.

    But, if either of the lines, "Global i As Int", or, "Global j As Int", is included in the code (not commented out), then, it won't compile, and you get the error message,

    Error 524 in C:\Users\root\Desktop\Toom\globals.bas(#:###): Undefined TYPE
    Line #: Global # as Int

    ' code -----------------------------------------------------------------------------------------
    
    #Compile Exe
    #Dim All
    
    'Global i As Int
    
    Sub s()
    'Global j As Int
    End Sub
    
    Function PBMain () As Long
    Global k As Int
    End Function
    
    Last edited by danbaron; 09-10-2011 at 07:32.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  2. #12
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi Dan,

    even the code you posted does not compile - simply because INT is not datatype in PowerBASIC. INT in powerBasic, as well as thinBasic, is function to "convert a numeric expression to an integral value".
    When you need integer variable in PowerBASIC, you have this possible variants:
    • INTEGER - for 16bit
    • LONG - for 32bit
    • QUAD - for 64bit

    The available datatypes are documented in PB/Help file under "Data Types" root chapter. Integral datatypes (both signed and unsigned) are documented in Data types/Integral data types.

    So the following compiles just fine...:
    #COMPILE EXE
    #DIM ALL
    
    GLOBAL i AS INTEGER
    
    SUB s()
      GLOBAL j AS INTEGER
    END SUB
    
    FUNCTION PBMAIN () AS LONG
      GLOBAL k AS INTEGER
    END FUNCTION
    
    Might be of interest that when using DIM, the compiler picks GLOBAL/LOCAL scope based on the declaration position:
    #COMPILE EXE
    #DIM ALL
    
    DIM i AS INTEGER ' -- Will become GLOBAL variable, as DIM is used in GLOBAL space
    
    SUB s()
      DIM j AS INTEGER ' -- Will become LOCAL variable, as DIM is used inside SUB/FUNCTION/METHOD space
    END SUB
    
    FUNCTION PBMAIN () AS LONG
      DIM k AS INTEGER ' -- Will become LOCAL variable, as DIM is used inside SUB/FUNCTION/METHOD space
      GLOBAL k2 AS INTEGER ' -- Using GLOBAL forces the variable to be GLOBALy visible
    END FUNCTION
    

    Petr
    Last edited by Petr Schreiber; 09-10-2011 at 09:14.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #13
    It will compile if you use the correct syntax

    #Compile Exe
    #Dim All
     
    Global i As Integer
     
    Sub s()
    Global j As Integer
    End Sub
     
    Function PBMain () As Long
    Global k As Integer
    End Function
    
    Int does not exist in the PowerBASIC language. Use Integer, if that is what the type you want, or Long, if its is a C++ Int.

  4. #14
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    You're right Petr, I'm stupid.

    And, the reason I thought the one case did compile was because, the "primary source file", was not the file I was viewing.

    Dan

    ---------------------------------------------------------

    So, then, the answer is yes, you can declare global variables from at least within subroutines, but, what good would doing that be?


    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  5. #15
    Petr has been quicker than I.

  6. #16
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    If I recall correctly, in Turbo Pascal you would make a project consisting of multiple files.

    Turbo Pascal always knew which files a particular project currently consisted of.

    Each of the files you could make into a unit.

    When you compiled the project (a menu command), only the units which had changed since the last compilation would be re-compiled.

    You didn't need to "#include" anything.

    You didn't need to "#compile" anything.

    You didn't need to "#link" anything.

    I agree that SLLs are good for "third-party" code.

    -------------------------------------------------------------------------------------------------

    Maybe that is why PowerBasic has always promoted how fast it compiles code, because, it would re-compile everything if even one character was changed.





    Last edited by danbaron; 09-10-2011 at 20:03.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  7. #17
    Oxygen supports the nesting of true subroutines; functions within functions, to a depth of 7 levels. (I trust that no sensible program would require more than 2 levels. )

    the static variables of the outer function are visible to its inner functions. But the local variables remain local to each function. I am not sure it would be desirable to make the outer locals directly visible to the inner function. And it is technically more tricky to do in a compiler.

    A static variable in this context is one which is located in the global work space of the program but its visibility is restricted to the function / procedure in which it is defined.

    function foo() as string
    
    
      static string s="okay"
    
    
      function boo() as string
    
    
        function hoo() as string
    
    
          static string t="hoo "
    
    
          return t+" "+s
    
    
        end function
    
    
        return hoo
    
    
      end function
    
    
      return boo
    
    
    end function
    
    
    print foo
    
    Charles

  8. #18
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    It executes like lightning.

    I saw the console program.

    I saw a file program.

    A lot of OOP.

    I think I need to be able to allocate memory and to assign a pointer to it, i.e., dynamic arrays.

    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

  9. #19
    So, then, the answer is yes, you can declare global variables from at least within subroutines, but, what good would doing that be?
    Seems to defeat the purpose of using global variables if they have to be declared (initialized) in each routine.
    Last edited by John Spikowski; 09-10-2011 at 17:23.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  10. #20
    You need one function to assign initial values to the global variables. By default they are set to null or zero.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Do you know: Logical variables
    By ErosOlmi in forum Do you know ...
    Replies: 1
    Last Post: 06-05-2011, 17:27

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •