forums

TNT Basic Forums > Bugs!
doubling variables
< Last Thread     Next Thread >
Author
Thread        Post A Reply

12-23-2007 16:56

Posted by:
Maxime

Location:
Paris France

Find more posts by Maxime

Not really a bug : it is possible to define twice the same variable, even with different values (prog takes the last one). The program runs normally !
This should be forbidden as a source of errors (I know, I did !).

12-24-2007 13:12

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

I actually think the way it is is easier incase you int i before every for loop like I do :|

12-28-2007 06:50

Posted by:
Maxime

Location:
Paris France

Find more posts by Maxime

Re-define

Thanks, Jacob
I think it is logical to initialize the loop variables on top of each PROCEDURE. This will avoid to use the same variable in a procedure called by an upper loop, which would certainly cause a problem like in :

Global int k
...
for k=1 to 10
MyProc()
next k
...
...
Procedure MyProc
for k= 11 to 20
...
next k
end proc

Adding a "int k" after the definition of MyProc is a good precaution which will avoid to mix the loops, even if you don't remember the first k exists (or if you create it days after...).
But initializing one variable before each loop does not seem profitable : if it exists, the "for" will initialize the value. If it exists not, the debugger will tell.
Evidently, you will find the same problem as on top if you use "goto's" to call another loop from inside the same procedure. But this is not good programming, and I would say DON'T DO !
Thanks for advice

12-28-2007 12:30

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Right the above code shouldn't work because, if k is global, then the second procedure will end with k=20. Then when the first loop runs again, k is greater than 10.

12-30-2007 16:13

Posted by:
Maxime

Location:
Paris France

Find more posts by Maxime

variables

YES ! That is exactly the error I wanted to show, and which can be not done if you redefine the loop variable you use at the head of each procedure.
If you have put "k" as a Global variable (which is not a good practice), the debugger would probably tell you that you cannot define also k as a local one, so , just change the name, but no error generated at run time.
If you have defined the first loop's k inside a procedure, the second will be "another" one, and no error will be generated any more.
So, seems me fine...
Happy New Year !

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.