Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

Global variables

English · Suomi · Русский

A variable is global if it is exported from a module. This usually refers to variables declared in a var section

Global variables can be accessed from all other modules that import the exporting modules. Note, however, that a program can not be imported.

pascal
program globalVariableDemo(input, output, stdErr);
    var
        x: integer;
    
    procedure doMagic;
    begin
        // here, x is global to doMagic
    end;
    
    procedure foo;
    var
        // shadow the global x
        x: integer;
    begin
        // here, x is local,
        // as the top-scope x can not be accessed
    end;
    
    // MAIN //
    begin
        // here, x is local
    end.

remarks

If speed matters, global variables are/were used for frequently invoked routines, since allocating local variables on the stack takes time. This, however, is considered bad style. Nest your variables as deep as possible, but as high as necessary.

A resourceString variable is always global.

The FPC supports thread variables, too. They are sort of half-way between global and local variables. A threadVar variable is local to a thread.

see also


From the Free Pascal wiki, page "Global variables", as the Internet Archive kept it on 2026-02-06, last changed on 2024-02-22. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .