View Full Version : Defining x and y as global variables
Is there anything wrong with this? I personally like to define them as global variables as it saves me the effort of defining them nearly every other function.
However, I've seen some members disapprove of this....
Is there anything wrong with this? I personally like to define them as global variables as it saves me the effort of defining them nearly every other function.
However, I've seen some members disapprove of this....
Just try to learn declaring them inside function because Global variables can mess up. If you use same Variable which is global over and over again. You can accidentally mess up something in your script.
(Bad Example. In some function X is declared as 10 and when next function comes it's still 10 because something went wrong in current function)
~Home
masterBB
04-09-2012, 09:51 PM
I'm not okay with this ;)
It's bad practice to declare global variables. Especially x, y and i. Those are used by a lot of functions and procedures. If for some reason one procedure changes it, the script might go wrong. Also it is bad practice for the eye, these are only used within the function and can be discarded afterwards. Not globally used to safe information where it is important that it isn't lost add the end of the function.
Kyle Undefined
04-09-2012, 11:09 PM
I really hate global variables, I usually try to stay away from them when I can.
Ok thanks guys - I'll stop doing it then :redface:
Getdropped
04-10-2012, 01:31 AM
Hehe I think you are talking about people disapproving of this was in my app ;) I like global variables, but that it cause in my opinion it is easier if I need the values later on in the script. Idc if its frowned upon, if it works it is good...... or least thats how i feel ;) however I'm sure it will catch up with me somewhere along the way in the programming life... so I should probably stop aswell :p
Harry
04-10-2012, 02:16 AM
however I'm sure it will catch up with me somewhere along the way in the programming life
It will ;) And then you wonder "why is my script doing this! it's so messed up!", you realize your globals are biting you in the butt. I used to use globals a lot, but you should really avoid them as much as you can, especially for coords and such.
smurg
04-10-2012, 02:40 AM
I really don't get this actually (even though i do declare mine locally).
I don't see how anything can mess up really unless you code like a cheeseball.
Why would you use X & Y in a function before declaring them anyways, either globally or locally?
Like who seriously does a FindColor (and respective family) function to define X, Y or a TPA of them and doesn't include the respective actions in it's own begin/end block.
Like if you do a:
FindColorSpiral(X, Y, ...)
Mouse(X, Y, ...)
Without checking, then it would mess up globally or locally anyways.
Imagine
04-10-2012, 02:49 AM
IMO globals should be used only for progress variables, (With some rare exceptions).
bolshak25
04-10-2012, 03:36 AM
i would only declare them globally if its value is used in other procedures/functions. you can declare them globally if you want, but locally makes it so it shouldnt go wrong by starting with a bad value.
Markus
04-10-2012, 09:17 PM
It's a really bad coding practice, there are a few good reasons why every comp sci class actively discourages using global vars. One of the reasons is, it starts with x and y and i, after that you do like x1, x2, y1 and y2 and you end up having only global vars.
Ideally you can cut and paste a function into a new script and it'll work. Won't work with stuff like this.
Also I'd like to invite everyone to also vote No at member apps if they spot this.
Also I'd like to invite everyone to also vote No at member apps if they spot this.
I got away with it then :D
putonajonny
04-10-2012, 09:35 PM
I would get as far away from global variables as possible look at the following case:
program new;
function DoubleThenAdd(a, b : integer) : integer;
Var
i, x, y : integer;
begin
x := a + a;
y := b + b;
Result := x + y;
end;
procedure DoSomething;
Var
i, x, y : integer;
begin
for i := 0 to 5 do
begin
x := x + 2;
y := y - 1;
WriteLn(DoubleThenAdd(x, y));
end;
end;
begin
DoSomething;
end.
program new;
Var
i, x, y : integer;
function DoubleThenAdd(a, b : integer) : integer;
begin
x := a + a;
y := b + b;
Result := x + y;
end;
procedure DoSomething;
begin
for i := 0 to 5 do
begin
x := x + 2;
y := y - 1;
WriteLn(DoubleThenAdd(x, y));
end;
end;
begin
DoSomething;
end.
They have different outputs just because of global/local vars, a very contrived case but could happen
Markus
04-10-2012, 09:48 PM
Also a bad example. Though pascalscript inits variables at 0, you shouldn't rely on that.
program new;
function DoubleThenAdd(a, b : integer) : integer;
Var
i, x, y : integer;
begin
x := a + a;
y := b + b;
Result := x + y;
end;
procedure DoSomething;
Var
i, x, y : integer;
begin
x := 0;
y := 0;
for i := 0 to 5 do
begin
x := x + 2;
y := y - 1;
WriteLn(DoubleThenAdd(x, y));
end;
end;
begin
DoSomething;
end.
Brandon
04-10-2012, 10:08 PM
Only time u use globals in Pascal, is if u need the value initialized from another function.. Or want the value preset before it hits ur second function since Pascal doesn't have pointers and all.. Well it does. Just not Simba.
In other languages like in the real world, u don't need globals ALL the time since u can use pointers.. but sometimes u do.
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.