Recent

Author Topic: Global Variable in program  (Read 2588 times)

simsee

  • Full Member
  • ***
  • Posts: 184
Global Variable in program
« on: October 04, 2022, 06:01:43 pm »
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][+][-]
  1. program Project1;
  2.  
  3. uses unit1;
  4.  
  5. var
  6.   Log : string;
  7.  
  8. begin
  9. end.  

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. procedure Proc;
  11.  
  12. implementation
  13.  
  14. procedure Proc;
  15. begin
  16.   writeln(Log);
  17. end;
  18.  
  19. end.  

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Global Variable in program
« Reply #1 on: October 04, 2022, 06:06:36 pm »
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

  • Hero Member
  • *****
  • Posts: 595
Re: Global Variable in program
« Reply #2 on: October 04, 2022, 06:09:17 pm »

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Global Variable in program
« Reply #3 on: October 04, 2022, 06:18:50 pm »
Wiki says: "variables declared .... prior any other nested block in a program", as in my example.

« Last Edit: October 04, 2022, 06:25:03 pm by simsee »

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Global Variable in program
« Reply #4 on: October 04, 2022, 06:32:49 pm »
Wiki says: "variables declared .... prior any other nested block in a program", as in my example.
And two lines after that:
Code: Text  [Select][+][-]
  1. Note, however, that a *program* can not be imported.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Global Variable in program
« Reply #5 on: October 04, 2022, 06:40:56 pm »
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

  • Hero Member
  • *****
  • Posts: 6676
Re: Global Variable in program
« Reply #6 on: October 04, 2022, 06:56:24 pm »
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.

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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Global Variable in program
« Reply #7 on: October 04, 2022, 06:57:34 pm »
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.

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

  • Hero Member
  • *****
  • Posts: 1038
Re: Global Variable in program
« Reply #8 on: October 04, 2022, 07:00:49 pm »
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.
That's right. Same as last year: https://forum.lazarus.freepascal.org/index.php/topic,53408.msg395022.html#msg395022
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Global Variable in program
« Reply #9 on: October 04, 2022, 07:20:26 pm »
I'm sorry.  I didn't remember asking already. :(

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Global Variable in program
« Reply #10 on: October 05, 2022, 01:29:51 am »
I'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

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Global Variable in program
« Reply #11 on: October 05, 2022, 01:41:28 am »
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
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Global Variable in program
« Reply #12 on: October 05, 2022, 08:49:11 am »
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....

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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Global Variable in program
« Reply #13 on: October 05, 2022, 09:50:28 am »
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
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".
Specialize a type, not a var.

Weiss

  • Full Member
  • ***
  • Posts: 127
Re: Global Variable in program
« Reply #14 on: October 06, 2022, 01:11:32 am »
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
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.

 

TinyPortal © 2005-2018