Thread: Trying Not To Use Global Variables With Function

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Trying Not To Use Global Variables With Function

    I want to have this function plus a variable by one. and it does. But i want to do it without using global variables, as i have read it is poor design. Does that mean you don't need to declare variables at all? I have seen this work that way (i think), but no matter what I seem to need to declare the int x in order to make this work.

    How can i make this work without global variables, and do i need to declare x at all?

    Code:
    #include<stdio.h>
    
    
    /*local function prototype*/
    int plus();
    
    /*main function*/
    int main(int argc, char * argv[])
    
    {
       
        x = 1;
        printf("x = %d \n",x);
        
        /*impliment the plus function*/
        x = plus(x);
    
        printf("\nx = %d",x);
        
    
        getch();
        return 0;
    }
    
    /*local function*/
    int
    plus(int input)
    {
        return input + 1;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Krash005 View Post
    I want to have this function plus a variable by one. and it does. But i want to do it without using global variables, as i have read it is poor design. Does that mean you don't need to declare variables at all? I have seen this work that way (i think), but no matter what I seem to need to declare the int x in order to make this work.

    How can i make this work without global variables, and do i need to declare x at all?

    Code:
    #include<stdio.h>
    
    
    /*local function prototype*/
    int plus();
    
    /*main function*/
    int main(int argc, char * argv[])
    
    {
       
        x = 1;
        printf("x = %d \n",x);
        
        /*impliment the plus function*/
        x = plus(x);
    
        printf("\nx = %d",x);
        
    
        getch();
        return 0;
    }
    
    /*local function*/
    int
    plus(int input)
    {
        return input + 1;
    }
    Declare x at local scope in main()

    i.e.

    Code:
    int main(int argc, char * argv[])
    {
        int x = 1;
    
       ...
    }
    No need for 'x' to be at global scope.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    But is that still bad design? I guess I am not really understanding why globals are bad, for locals are okay.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Globals are bad coz if they unintentionally get changed in some other function then it'll be messy. I dont know better reason than that, but surely if someone can clarify more on it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Global variables requires a carefull mind of the programmer, but its not necessarily bad design. The same applies to goto, which is highly useful in error correction in the lack of better methods, like exceptions.

    Global variables can be modified at any time, resulting in potentially unpredictable result in case of unindended modifications, and it can be harder to discover the source of the error. This is the reason its classified as bad design, however, the fact remains that there are not major principal difference between a file scope global variable in C or a class scope variable in C++ or Java.

    What you should do is keep the scope of the global variable to the actual code space where its needed, in C this usually refers the the C file where its defined. To do that you decalare it static:

    Code:
    static int g_Variable;
    Now the variable is not visible outside the C file. Personally I keep all code space variables in a static struct, marking local to that perticular scope, like this:

    Code:
    static struct{
        int a;
        int b;
    }locals;
    Then you can access the locals in the code scope like this locals.a or locals.b, which is in my opinion no different from class scope variables in other languages and you never hear anything about bad design.
    Last edited by glennik; 12-03-2009 at 02:27 PM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I agree with glennik. There are circumstances where not using a global can be much more awkward than using one. There are also libraries that force you to do so, because they involve callbacks that do not take ANY parameters.

    However, as zacs7 points out, your case is not one of these, you do not need a global, so don't use one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by glennik
    Global variables requires a carefull mind of the programmer, but its not necessarily bad design.
    While I can agree with this statement...

    Quote Originally Posted by glennik
    This is the reason its classified as bad design, however, the fact remains that there are not major principal difference between a file scope global variable in C or a class scope variable in C++ or Java.
    (...)
    Then you can access the locals in the code scope like this locals.a or locals.b, which is in my opinion no different from class scope variables in other languages and you never hear anything about bad design.
    Class scope variables in such languages, especially if they have non-private access, are symptomatic of bad design, for the same reasons as global variables. One should not use such language features to broadly justify global variables. I would rather justify the use of global variables by considering that for some programs, the presence of globally shared data (rather than just global constants) may be inherent in the problem that the program is supposed to solve, and reuse for non-global data is regarded as very improbable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Quote Originally Posted by laserlight View Post
    While I can agree with this statement...


    Class scope variables in such languages, especially if they have non-private access, are symptomatic of bad design, for the same reasons as global variables. One should not use such language features to broadly justify global variables. I would rather justify the use of global variables by considering that for some programs, the presence of globally shared data (rather than just global constants) may be inherent in the problem that the program is supposed to solve, and reuse for non-global data is regarded as very improbable.
    Compared to what you are referring to, using a class scope variable with public access, would i C be using an extern decalaration, which is pretty much the opposite of what I stated.

    However, I should have elaboreted upon the detail that I was aiming at private class scope variables, as compared to global variables with C file scope, which in my opinion, does not represent any major principal difference.

    And, I never said that laguage features, like declaring a global variable static, justifies a broad use of global variables. But, in real life applications, global variables at some level are necessary and then you should use the tools in the toolbox to limit the possible undesired consequences, and using a static struct is such a tool, and not a general justification of global variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  3. Global variables sent to a function
    By ExtremelyStupid in forum C Programming
    Replies: 5
    Last Post: 08-02-2004, 05:57 AM
  4. Replies: 4
    Last Post: 10-17-2002, 10:09 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM