Thread: Global Variables

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Global Variables

    If I have global variables, do I need to put them as a parameter of the function and will their value be changed by any function to be used by another function with using return?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Global variables are changable within the program (and they can be had with extern by other files in your project) so it is unnecessary to pass them in functions. You also don't return anything global.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Good just as I thought, and it doesn't matter now because I found my mistake and realised that I was right about global variables afterall.

    What is so bad about using them? As most books seem to shy away from them.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's all about design. If you find yourself passing the exact same structure or something to a lot of functions, or you need to have access to something accross lots of files, then it probably deserves to be global and you have a decision to make. But that's it.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    The problem with global variables is that they can be modified globally, by any function anywhere in your program. This can create debugging nightmares in larger projects and is one major reason for projects being completely scrapped. This is why they are highly advised against using.

    If they declared as const global variables the problem goes away, however. But you really wouldn't call them variables anymore, they'd be called global constants, which I think are O.K.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM