Recent

Author Topic: Preset global variable  (Read 4298 times)

coradi

  • Full Member
  • ***
  • Posts: 148
Preset global variable
« on: October 14, 2021, 10:10:11 pm »
Why this doesn't work?
Code: [Select]
Program Test;
 Var i : Byte = 5;
Brgin
end.

I have read
"The declaration of a global variable differs in one way from the declaration of a local variable. Namely, you can assign a value to a global variable directly at the declaration, an initial value. This is done as follows (as an example):"
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

coradi

  • Full Member
  • ***
  • Posts: 148
Re: Preset global variable
« Reply #1 on: October 14, 2021, 10:14:09 pm »
oh, ok.
I see. Now it is a constant....
Then it works with that...my problem was, that there was an error mesage, because I use
For I := 1 To 100 Do


hmmm..no, not really a constant..
I can do
I:=0;
But it is not allowed to do
For I := 1 To 10 Do..


ok
« Last Edit: October 14, 2021, 10:17:42 pm by coradi »
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Preset global variable
« Reply #2 on: October 14, 2021, 10:36:42 pm »
For loops need a local variable, not a global.

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Preset global variable
« Reply #3 on: October 15, 2021, 03:47:05 pm »
Why this doesn't work?
Code: [Select]
Program Test;
 Var i : Byte = 5;
Brgin
end.

What do you mean with "Why this doesn't work"? Do you get a compile time error? Do you get an exception? Please provide a full example of what you're trying to achieve.

For loops need a local variable, not a global.

With the exception of the main function ;)

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Preset global variable
« Reply #4 on: October 15, 2021, 04:10:50 pm »
Is the "Brgin" a copy&paste error to the forum post or in your code? In the latter case, the compiler won't accept it because it is expecting "Begin"

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Preset global variable
« Reply #5 on: October 16, 2021, 11:05:33 am »
For loops need a local variable, not a global.

With the exception of the main function ;)

In which case my memory is obviously faulty: I could have sworn I've seen the compiler object.

...I've just checked with a trivial program (no mode override) and a global does work in that particular case. Which is odd, unless some version of FPC older than 3.0.4 didn't like it.

My apologies to OP for bad advice.

MarkMLl
« Last Edit: October 16, 2021, 11:09:42 am by 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

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Preset global variable
« Reply #6 on: October 16, 2021, 04:42:34 pm »
In which case my memory is obviously faulty: I could have sworn I've seen the compiler object.

...I've just checked with a trivial program (no mode override) and a global does work in that particular case. Which is odd, unless some version of FPC older than 3.0.4 didn't like it.

How would you then have for loop if there isn't funtions and procedures?
Or do you know how to declare local variables for main function?  :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Preset global variable
« Reply #7 on: October 16, 2021, 05:03:24 pm »
How would you then have for loop if there isn't funtions and procedures?

By not allowing it in the main program body. I presume.

As I've said... ADMITTED, dagnabbit... my memory's obviously faulty on this. But I do admit to being a little puzzled as to how I got to this position.

Without going back and searching code, I suspect that I'm thinking about the initialisation part of an imported unit. I tend not to use these heavily except for assertions checking any important assumptions about record sizes and bit ordering, and I suspect that I might have had to move code with a for loop into its own function... @PascalDragon, does that sound about right?

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

 

TinyPortal © 2005-2018