When programming, it is important to know which variables you can use where. I'll explain.
Let's say you declare a global integer x, and you define a function myFunct(). In the definition of myFunct(). You would obviously be able to use this integer x. But does it go the other way around? That is, can you use variable declared inside a function outside the function?
Simple answer. No.
When you call a function, the "flow" of your program halts so that it can go run your function and then return with the results. Let's say we have a function add2()
int add2(int x)
{
int y = 2;
return x+y;
}
Yes, I know there's no reason to declare this variable y, but it's for the sake of the example. In this case, The variable called y only "lives" until the end of the function. After that, this variable no longer exists, and hence it can no longer be referred to (Things like this can be overcome using the "static" keyword. I'll mention this sometime). In fact, this variable x no longer exists either. x is just a temporary holder for a value that is used while the function is running!
Functions are just one example of where knowledge of a scope could be useful. Let's say you were running a for loop.
int i;
for(i=0;i<5;i++)
{
//do stuff
}
Clearly, this is just an average for loop. Runs whatever code between its curly brackets 5 times. Now check this out:
for(int i=0;i<5;i++)
{
//do other stuff
}
Can you see the difference?
The difference is that in the first case, the variable i would be usable even after the for loop was executed. In the second case, i "dies" as soon as the loop terminates. Personally, I like to use the second method just because it allows me to use the same identifier for all my loops (assuming they're not nested) without fear of that identifier already being declared and used for another loop. I also just find it neater ;)
Sending this to my computer science major of a girlfriend, maybe she'll benefit. =p
ReplyDeleteStatic variables love scopes. I just programmed a class that used static members. It was a fun experience. Try it out.
ReplyDeleteOooooh....That should make things much cleaner.
ReplyDeleteapparently this looks very similar to Java.. hmm thanks!
ReplyDeleteWhen I am programming I always look for the most beneficial algorithm, thanks Following!
ReplyDeleteVery informative! Followed.
ReplyDeleteHahaha I see this stuff all the time in source code!
ReplyDeleteI think I might forward this blog to my computer science friend.
ReplyDeleteThis is exacly what I have been looking for, followed
ReplyDeleteThis looks harder than trying to read chinese.
ReplyDeleteThat helps a bit. Was having issues with int codes. Followed
ReplyDeleteOnce again, a great tutorial!
ReplyDeleteThanks.
I swear trying to figure this stuff out will make me go nuts! Thanks for the help eitherway though, I'll try to remember the tips.
ReplyDeletenice I found it quite similar to delphi
ReplyDeleteA great read! Not so much of a programmer but it's nice to read about things nonetheless. Really challenges you to think logically. :)
ReplyDeletebasic programing again but its always worth to review the basics every now and then to make better programs.
ReplyDeleteI'm gonna have to show this to my friend. He's going to school for this kind of stuff.
ReplyDeleteWow a C programming blog! I have been learning C for some time now it has been very difficult. - followed
ReplyDeleteStarting to see a bit more about this than I had in the past. Thanks for the post.
ReplyDeleteThis'll come in handy for my Comp Programming class, followin :)
ReplyDeleteVery informative! Flying Toasters!
ReplyDeletei can't say i understand all of it, but it's really interesting. followed!
ReplyDeleteHope you make more post! Kind of encouraged me to continue with C.
ReplyDeleteinteresting, like to hear more about you!!
ReplyDeletefollowing :)
im not sure did i understand this :P
ReplyDeleteSeriously interesting stuff here!
ReplyDeleteOnly programming languages I've used are blitzmax and java, haven't got enough patience to learn C. :)
ReplyDeleteI really should get back to C. I'm pretty sure I'll need it sometimes. Thanks for the tutorials.
ReplyDeleteI'm learning Java, but those things seem exactly the same! hahaha...
ReplyDeletefollowed!
can you start with the VERY begginning :s
ReplyDeletebut also, what program exactly do you use for this?
I wish I could see the difference :|
ReplyDeleteThanks for the info, I'm getting into C++ myself, and this knowledge will be used! Thanks!
ReplyDeleteHuge difference declaring in and out of the for loop!
ReplyDeletehad a look through a couple of your posts, you're clearly trained in computer science,... what do you do for a profession?
ReplyDeletefollowed you, hit me back, follow!
Being a computer science major, I can say this should help beginners!
ReplyDeletePerfect, thanks for this!
ReplyDeleteCan you see the difference?
ReplyDeleteordering Prozac online site
ordering Soma buysomaonlinenorx.org