Global Variable in program lazarus / 61-Beginners ---------------------------------------------------------------------- simsee · 2022-10-04 16:01 I apologize for the trivial question. The documentation says (see: https://www.freepascal.org/docs- html/current/ref/refse21.html#x52-720004.1) that "Global variables are variables declared in a unit or program, but not inside a procedure or function. " If I have the following program and unit, why is the local variable log not visible in the proc procedure of unit1? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; uses unit1; var  Log : string; beginend. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils; procedure Proc; implementation procedure Proc;begin  writeln(Log);end; end. ---------------------------------------------------------------------- Bogen85 · 2022-10-04 16:06 The program Project1 has no interface section that Unit1 can use (programs don't have an interface), to let Unit1 know about the global variable name. ---------------------------------------------------------------------- Bogen85 · 2022-10-04 16:09 See https://wiki.freepascal.org/Global_variables ---------------------------------------------------------------------- simsee · 2022-10-04 16:18 Wiki says: "variables declared .... prior any other nested block in a program", as in my example. ---------------------------------------------------------------------- alpine · 2022-10-04 16:32 Quote from: simsee on October 04, 2022, 06:18:50 pmWiki says: "variables declared .... prior any other nested block in a program", as in my example. And two lines after that: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Note, however, that a *program* can not be imported. ---------------------------------------------------------------------- simsee · 2022-10-04 16:40 Now I understand. Program modules do not export identifiers, as unit interface sections do.  So the only way to have a global variable, in the sense of visible to all modules (the program and the units), is to declare it in the interface section of a unit, which must be included in the uses clause of all modules. ---------------------------------------------------------------------- MarkMLl · 2022-10-04 16:56 Quote from: simsee on October 04, 2022, 06:40:56 pmNow I understand. Program modules do not export identifiers, as unit interface sections do.  So the only way to have a global variable, in the sense of visible to all modules (the program and the units), is to declare it in the interface section of a unit, which must be included in the uses clause of all modules. That's right, with one caveat. This is /highly/ implementation- specific, but in some cases you can export an identifier (e.g. a function name) from the main unit, and use that as an entry point if e.g. a dynamically-loaded library wants to call back into the main program. I emphasise, however, that that is outside the Pascal language as defined. So as you say, if you need a globally-accessible variable visible to the main program then you have to put something in the interface part of some other unit: as a particular case, that's what the Lazarus IDE does with the variables representing GUI forms/windows. I say "something" above advisedly, since it might either be a variable or a property (the latter case allowing a certain amount of access control. HTH MarkMLl ---------------------------------------------------------------------- Bogen85 · 2022-10-04 16:57 Quote from: simsee on October 04, 2022, 06:40:56 pmNow I understand. Program modules do not export identifiers, as unit interface sections do.  So the only way to have a global variable, in the sense of visible to all modules (the program and the units), is to declare it in the interface section of a unit, which must be included in the uses clause of all modules. Yes, your understanding is correct (with MarkMLI's caveat of course). And the same global identifier can be exported from different units, and does not refer to the same identifier globally. If there is a confusion you can correct this by uses units ordering, or by prefixing the identifier with the unit name it came from. ---------------------------------------------------------------------- alpine · 2022-10-04 17:00 Quote from: simsee on October 04, 2022, 06:40:56 pmNow I understand. Program modules do not export identifiers, as unit interface sections do.  So the only way to have a global variable, in the sense of visible to all modules (the program and the units), is to declare it in the interface section of a unit, which must be included in the uses clause of all modules. That's right. Same as last year: https://forum.lazarus.freepascal.org/ index.php/topic,53408.msg395022.html#msg395022 ---------------------------------------------------------------------- simsee · 2022-10-04 17:20 I'm sorry.  I didn't remember asking already. ---------------------------------------------------------------------- Zath · 2022-10-04 23:29 Quote from: simsee on October 04, 2022, 07:20:26 pmI'm sorry.  I didn't remember asking already. It can happen. Going back to old, unfinished projects often brings the same questions. I go downstairs from my office to the warehouse and have forgotten what I went for. I then sit back at my desk, see some paperwork and think "oh yeah", that's it !! Usually a few expletives too, along the way. ---------------------------------------------------------------------- dbannon · 2022-10-04 23:41 I prefer the term "regional variables" when they are shared within a unit and only call them Global in the very rare case where they are accessible across all relevant units. Might seem like playing with words but there is quite a difference.... Davo ---------------------------------------------------------------------- MarkMLl · 2022-10-05 06:49 Quote from: dbannon on October 05, 2022, 01:41:28 amI prefer the term "regional variables" when they are shared within a unit and only call them Global in the very rare case where they are accessible across all relevant units. Might seem like playing with words but there is quite a difference.... Well, global has been used in this context for rather a long time, and a pointer to an unexported global will, generally speaking, be valid during the entire lifetime of the program. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-10-05 07:50 Quote from: dbannon on October 05, 2022, 01:41:28 amI prefer the term "regional variables" when they are shared within a unit and only call them Global in the very rare case where they are accessible across all relevant units. Might seem like playing with words but there is quite a difference.... Davo Actually you can achieve that by creating a unit with your "regional" code and only use it in the uses clause of the implementation sections of the units in your "region". ---------------------------------------------------------------------- Weiss · 2022-10-05 23:11 Quote from: Thaddy on October 05, 2022, 09:50:28 amQuote from: dbannon on October 05, 2022, 01:41:28 amI prefer the term "regional variables" when they are shared within a unit and only call them Global in the very rare case where they are accessible across all relevant units. Might seem like playing with words but there is quite a difference.... Davo Actually you can achieve that by creating a unit with your "regional" code and only use it in the uses clause of the implementation sections of the units in your "region". I think I need an example. Global variables behavior was bugging me lately too. Especially when I expand on original code to add a new feature. ---------------------------------------------------------------------- Thaddy · 2022-10-06 06:37 example is forthcoming. couple of hours. currently in meeting. ---------------------------------------------------------------------- dbannon · 2022-10-06 06:56 sorry Thaddy, I don't do Business Meetings anymore  Quote from: Weiss on October 06, 2022, 01:11:32 amI think I need an example. Global variables behavior was bugging me lately too. Especially when I expand on original code to add a new feature. We all get a bit sensitive about the term, Global Variables, I suggest because when badly done, for example, early BASIC code, it made for a confusing system to maintain. But here we can, with care, work better. Hmm, lets assume you have a project that uses several units. And all units have nice interface and implementation structure.  Lets assume one unit has - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i interfacetype    TClassA = class    .....    public        MyAVar : integer;        ......type    TClassB = class    ....    private      MyBVar : integer;        ......     var        MyGlobalVar : integer; implementation...var MyRegionalVar : integer;...//code for methods of TClassA  and TClassB Now, there are four "global-ish" variables there, showing quite different characteristics. Easy stuff first, MyBVar can only be used within TClassB methods so, I suggest its are not really global at all.    MyAVar can be used within the TClassA methods AND can be used by anything that has access to an instance of TClassA. For every instance of TClassA, there is a MyAVar.  But MyRegionalVar is a different thing altogether !  Its a global, there is only one variable in existence and that can only be used within TClassA and TClassB. Definitely more global than above but, importantly, another unit that 'uses' this unit cannot see MyRegionalVar. Finally (*) we have MyGlobalVar, because its declared in the interface, it can be used by any unit that 'uses' this one. Yep, thats really Global ! (*) There are a number of other things possible here too, Class variables for example, just outside this lecture  So, why are Globals bad ?  The real problem is when reading code, you can be left wondering if some process, maybe in a completely unexpected place, is changing the value of that variable. And wondering what will be affected if you alter its value. This is not so much of a problem in a "regional", you are looking at the only unit that uses it but is still well avoided if possible, especially if its a big unit. Before we use a Global, consider using things like functions that return the value of a local variable (ie a MyBVar) to other units that need only read access or Properties that can control access and thus take away a lot of the mystery. But if you do decide to use a Global, make sure its well documented - "This is a global declared in UnitXX and tells us how many eggs broke, only UnitYY should change it." , maybe as a comment every time its used ! If its hard work using Globals, maybe you will look a bit harder to find an alternative  . Davo ---------------------------------------------------------------------- Thaddy · 2022-10-06 07:09 Not business, charity fund. ---------------------------------------------------------------------- alpine · 2022-10-06 08:10 @dbannon Shouldn't the terms Visibility and Lifecycle be introduced first, and then asked what the Global means to the OP? Because IMHO for the beginners, the great deal of confusion comes from the misunderstanding of those. ---------------------------------------------------------------------- MarkMLl · 2022-10-06 08:39 Quote from: y.ivanov on October 06, 2022, 10:10:17 amShouldn't the terms Visibility and Lifecycle be introduced first, and then asked what the Global means to the OP? Because IMHO for the beginners, the great deal of confusion comes from the misunderstanding of those. I agree, although I think "lifetime" is a bit more common in English for this. Also in Pascal (unlike C etc.) "scope" and "visibility" are distinct because of the possibility of nested function definitions... in fact "scope" and "lifetime" are arguably equivalent. MarkMLl