non global variables

This is a discussion on non global variables within the C++ Programming forums, part of the General Programming Boards category; I'm using the Dev C++ 5 compiler and for all the time I've been programming I've been using global variables. ...

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    Post non global variables

    I'm using the Dev C++ 5 compiler and for all the time I've been programming I've been using global variables. I've always read that it's dangerous and now I've come across a time when errors have occured. The problem is that I can seem to be able to use a variable from int main() without causing errors.

    Code:
    #include <iostream>
    using namespace std;
    void function(int x);
    
    void function(int x)
    {
         x = 5;
         cout<<x;
         cin.get();
    }
    
    int main()
    {
        int x;
        function();
    }
    when i try compiling this the errors:
    In functionmain too few arguements to function void function[int]
    at this point in file.

    Code:
    #include <iostream>
    using namespace std;
    void function(int x);
    
    void function(int x)
    {
         x = 5;
         cout<<x;
         cin.get();
    }
    
    int main()
    {
        int x;
        function(int x);
    }
    And this says:
    syntax error before ) token

    Please tell me what is wrong and how to do it.
    Thanks in advance
    I started out with nothing and I still have most of it left.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You don't need to specify the type on an actual parameter only on the formal parameter:
    Code:
    #include <iostream>
    using namespace std;
    void function(int x);
    
    void function(int x)
    {
         x = 5;
         cout<<x;
         cin.get();
    }
    
    int main()
    {
        int x;
        function(x);
    }
    This passes a copy of x to function(). Since its a copy any changes made to x inside of function() do not affect the x inside of main(). This is known as passing by value.

    To have function() affect a change upon the variable x in main() you need to pass a reference. To do that you have to change function() to accept a reference like so:
    Code:
    void function(int &x)
    Now any changes made to x inside of function() affect the variable that was passed into it. Its important to note that the names do not matter. To see this try running the following:
    Code:
    #include <iostream>
    using namespace std;
    void function(int x);
    
    void function(int x)
    {
         x = 5;
         cout<<x<<endl;
    }
    
    int main()
    {
        int z;
        function(z);
        cout<<z<<endl;
        cin.get();
    }
    Last edited by Thantos; 01-10-2005 at 12:07 PM.

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, 03:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21