variable modification inside functions

This is a discussion on variable modification inside functions within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to find a way to let functions edit variables outside of main(). Is there anyway to ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    variable modification inside functions

    Hello,

    I am trying to find a way to let functions edit variables outside of main(). Is there anyway to do this?

    My code currently look like this

    Code:
    #include <iostream>
    
    using namespace std;
    
    int Runme(int y, int z); 
    
    int main()
    {
    int y; y = 0; //define variables
    int z; z = 0;
    int a; a = 0;
    
    
    cin>> y;
    cout<<"\n"<<y<<"\n";
    
    Runme(y, z);
    
    cout<<"\n"<< y <<" "<< z;
    cin >> a;
    
    }
    
    int Runme(int y, int z)
    {
       cin>>z;
       cout <<"\n"<<y<<" "<<z;
       y = y + z;
       cout <<"\n"<< y <<" "<< z;
    return y, z;
    }
    If you complie this, you'll notice the last cout statement shows int y and z as they are in main, not with the last input recived from Runme()...

    Is there anyway for function Runme() to pass it's variables back to main()? What would someone recommend I do when trying to work with variables inside main() like this?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    The problem is the variables y and z in your function "Runme" are just copys of y and z from main, not the originals. To modify the variables like you are asking one way would be to pass them by reference.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void Runme(int &y, int &z); 
    
    int main()
    {
    	int y = 0; //define variables
    	int z = 0;
    	int a = 0;
    
    	cout << "Enter a value for \"y\" in main : " << endl;
    	cin >> y;
    	cout << "\n\"y\" in main " << y << endl;
    
    	Runme(y, z);
    
    	cout << "\n\"y\" and \"z\" in main after Runme " << y << " " << z << endl;
    	cin >> a;
    }
    
    void Runme(int &y, int &z)
    {
    	cout << "Enter a value for \"z\" in Runme : " << endl;
    	cin >> z;
    	cout << "\n\"y\" in Runme " << y << " z in Runme "<< z << endl;
    	y = y + z;
    	cout << "\nAfter y = y + z " << y << " " << z << endl;
    	//return y, z;
    }

  3. #3
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    if you want to edit the variables in main directly from a function, then include them in the functions argument and add a & infront of them
    Code:
    int Runme(&y, &z);
    I'm not sure if you need to include those & when you declare the function. Try it out and write back.

  4. #4
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    you got there before me

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation There are two issues...

    1- When you pass a variable into a function, you are not passing the variable... you are only passing it's value. This is called passing by value. That value is assigned to a new variable inside the function. Sometimes the variable inside the function has the same variable-name, but it's a different X and a different Y!

    2- A function can only return one value. Again, it's returning a value of a particular type.

    There are two ways around this - Use pointers or better yet, references which allow you to "get-to" all of the variables by passing-in their addresses. Hmmm... no reference tutorial???

    Most beginning C++ books will introduce pointers and references with a function that swaps the values of X & Y. You can't do that with regular variables.

    Pointers are used with C-style strings, because a function cannot return an entire array of characters.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    Some other comments:

    1. Your initialization of y is unnecessary. You don't do anything with it before the user stuffs a value into it so it's not critical that it have a value prior to that point (although it doesn't hurt either).

    2. The a variable is unnecessary. It seems you really only have it as a method of pausing the program before exiting by getting user input. A simple cin.get(); call should work just as well.

    3. As far as z goes, you can declare and initialize the variable all in a single step by just saying int z = 0; instead of having two statements to declare and then initialize.
    I used to be an adventurer like you... then I took an arrow to the knee.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by hk_mp5kpdw
    Some other comments:

    1. Your initialization of y is unnecessary.
    In my opinion, telling beginners not to initialize their variables is bad advice.

    Always initialize your variables, period.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 01-12-2006, 11:00 PM
  3. Replies: 20
    Last Post: 11-12-2005, 02:10 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Accessing CEdit Member Functions Inside CEditView ::MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-10-2002, 08:52 PM

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