Thread: Global variables, or...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Global variables, or...

    Should I use global variables or passing the variable to other functions?

    I do need a variable to all the program, now, I did hear that using global-variables isn't a nice thing, now I think in some case's is better to use global-variables instead passing them trough all the functions, look:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //my global variable
    char name[100];
    
    [...]
    int main() 
    {
    //here I use my global var..
    [....]
    Or should I make in this way:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(char name[], int Blah, int Bloh);
    
    int main(void)
    {
         char name[100]; //here isn't global
         [...]
         foo(name,myNum1,myNum2);
         [...]
       return 0;
    }
    Thanks for the help

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You didn't really give a very good explanation of the situation, but considering it's a "name," you almost definately want to pass the pointer to the first element to the function rather than using a global variable. Again, with the limited information it's hard to say, but there are only very rare circumstances that you'd want to ever use globals, and this doesn't appear to be one of them with the little information that you have provided.

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Try not to use redundant global variables. They take up space during the whole program, and that's not good
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I think that using local variables and passing them to functions is better, since you keep each variable in a limitted scope.
    none...

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Especially in large programs the use of global variables makes it hard to maintain the code. Then you have to search a lot where a certain, not local or passed, variable in a function comes from and what type it has etc.

    Also passing parameters to functions makes the functions less dependent. For example, if you have written a function which adds to numbers and the numbers to be added are stored in global variabels, then if you want to reuse the function, then you are required to have extra global variables in your program. Now imagine that most of the functions you want to reuse require global variables, that would mean you need a lot of global variables. Also other functions must now support those global variables.

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