problem with variables used for a FOR loop not being stored correctly

Previous topic - Next topic

Gary

I have found what I think is an issue with passing a variable to a subroutine.  Example code

Code (glbasic) Select

GLOBAL i

FOR i = 0 TO 10
GOSUB printi
NEXT

END

SUB printi:
DEBUG i+"\r\n"
ENDSUB


I would expect the output to be
0
1
2
3
4
5
6
7
8
9
10

but instead I get
0
0
0
0
0
0
0
0
0
0
0

if I make a copy of the variable used in the for loop it passes to the routine correctly.

It almost looks like a variable used in a for loop is not the same variable as the globally defined one. See the code below for a better explanation

Code (glbasic) Select

GLOBAL i=100

FOR i = 0 TO 10
GOSUB printi
NEXT

END

SUB printi:
DEBUG i+"\r\n"
i=i+1
ENDSUB


this will display
100
101
102
103
104
105
106
107
108
109
110

in the debug window

Using a function also does the same thing

Gary

Crivens

I've had similar things in the past where I found assigning the for variable to another variable and using that variable instead seemed to work. Don't know why and I can't remember if I reported it at the time to be honest

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

Gary

just ran another test and found its not related to subroutines

Code (glbasic) Select

GLOBAL i=100

FOR i = 0 TO 10
NEXT
DEBUG i+"\r\n"
END


will output i being = 100 (the default value) after the for loop is run when it should be 10

Kitty Hello

With option implicit, the for loop will generate a local variable in the loop scope. With explicit the i global should be used.

Gary

explicit is set in the build options, toggling it off makes no difference.

Or is there another setting elsewhere that sets it?

kanonet

Even when Gernot said it different, im sure FOR always defines a new, local variable, which is why you dont need to define it. If you already have a global with the same name, than the loop uses the local, but your sub the global of cause (since there is no local with than name defined there, but even if it where, it would be a local - not the same local like in the loop). If you want to use your For-variable (in this case the i), just give it as a parameter to you sub.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrTAToad

It will always print a value from 100 - the i% in the FOR loop is defined as local, and will not display the value for that variable in the subroutine.  Changing to and from explicit declaration doesn't make a difference.

This is the correct way of how variable work, I believe - it does show a limitation of subroutines over functions :)

Hemlos

i found a bug with ide and for, and inline
if you edit an inline with a 'for' in the code:

inline
for(i blah blah)
endinline

if you edit the inline and move something, sometimes it turns into this FOR

inline
FOR()
endline

This will give alot of bad errors in the precompiler, with no pointer to the problem.
I problem and actively check for this myself by eye, and i dont think its a critical problem, personally.
Bing ChatGpt is pretty smart :O

MrTAToad

Yes, that can be annoying :)  It's where the IDE isn't checking to see whether the command is inside an INLINE block or not...

Another annoyance is where variables (or even functions) are converted to the layout of another variable or function of the same name...

For example I have a function called Setup, and a variable called setup.  Unfortunately "setup" gets converted to "Setup"