Lazarus

Free Pascal => Beginners => Topic started by: coradi on October 14, 2021, 10:10:11 pm

Title: Preset global variable
Post by: coradi 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):"
Title: Re: Preset global variable
Post by: coradi 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
Title: Re: Preset global variable
Post by: MarkMLl on October 14, 2021, 10:36:42 pm
For loops need a local variable, not a global.

MarkMLl
Title: Re: Preset global variable
Post by: PascalDragon 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 ;)
Title: Re: Preset global variable
Post by: wp 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"
Title: Re: Preset global variable
Post by: MarkMLl 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
Title: Re: Preset global variable
Post by: dseligo 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?  :)
Title: Re: Preset global variable
Post by: MarkMLl 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
TinyPortal © 2005-2018