Sunday, 20 March 2011

Scopes

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 ;)

37 comments:

  1. Sending this to my computer science major of a girlfriend, maybe she'll benefit. =p

    ReplyDelete
  2. Static variables love scopes. I just programmed a class that used static members. It was a fun experience. Try it out.

    ReplyDelete
  3. Oooooh....That should make things much cleaner.

    ReplyDelete
  4. apparently this looks very similar to Java.. hmm thanks!

    ReplyDelete
  5. When I am programming I always look for the most beneficial algorithm, thanks Following!

    ReplyDelete
  6. Very informative! Followed.

    ReplyDelete
  7. Hahaha I see this stuff all the time in source code!

    ReplyDelete
  8. I think I might forward this blog to my computer science friend.

    ReplyDelete
  9. This is exacly what I have been looking for, followed

    ReplyDelete
  10. This looks harder than trying to read chinese.

    ReplyDelete
  11. That helps a bit. Was having issues with int codes. Followed

    ReplyDelete
  12. Once again, a great tutorial!
    Thanks.

    ReplyDelete
  13. 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.

    ReplyDelete
  14. nice I found it quite similar to delphi

    ReplyDelete
  15. A great read! Not so much of a programmer but it's nice to read about things nonetheless. Really challenges you to think logically. :)

    ReplyDelete
  16. basic programing again but its always worth to review the basics every now and then to make better programs.

    ReplyDelete
  17. I'm gonna have to show this to my friend. He's going to school for this kind of stuff.

    ReplyDelete
  18. Wow a C programming blog! I have been learning C for some time now it has been very difficult. - followed

    ReplyDelete
  19. Starting to see a bit more about this than I had in the past. Thanks for the post.

    ReplyDelete
  20. This'll come in handy for my Comp Programming class, followin :)

    ReplyDelete
  21. Very informative! Flying Toasters!

    ReplyDelete
  22. i can't say i understand all of it, but it's really interesting. followed!

    ReplyDelete
  23. Hope you make more post! Kind of encouraged me to continue with C.

    ReplyDelete
  24. interesting, like to hear more about you!!
    following :)

    ReplyDelete
  25. im not sure did i understand this :P

    ReplyDelete
  26. Seriously interesting stuff here!

    ReplyDelete
  27. Only programming languages I've used are blitzmax and java, haven't got enough patience to learn C. :)

    ReplyDelete
  28. I really should get back to C. I'm pretty sure I'll need it sometimes. Thanks for the tutorials.

    ReplyDelete
  29. I'm learning Java, but those things seem exactly the same! hahaha...

    followed!

    ReplyDelete
  30. can you start with the VERY begginning :s

    but also, what program exactly do you use for this?

    ReplyDelete
  31. I wish I could see the difference :|

    ReplyDelete
  32. Thanks for the info, I'm getting into C++ myself, and this knowledge will be used! Thanks!

    ReplyDelete
  33. Huge difference declaring in and out of the for loop!

    ReplyDelete
  34. had a look through a couple of your posts, you're clearly trained in computer science,... what do you do for a profession?
    followed you, hit me back, follow!

    ReplyDelete
  35. Being a computer science major, I can say this should help beginners!

    ReplyDelete
  36. Perfect, thanks for this!

    ReplyDelete
  37. Can you see the difference?
    ordering Prozac online site
    ordering Soma buysomaonlinenorx.org

    ReplyDelete