Thread: Is it possible to change the value of a variable in main using a function?

  1. #1
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20

    Question Is it possible to change the value of a variable in main using a function?

    Hi, I'm currently writing a poker game and am trying my best to avoid using global variables. I have a few variables in int main() which i was hoping to use to store the value of each players hand. I then created a function which calculates the value of the hand but cannot get this value back into the main function.

    For example:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void getValue(int value)
    {
        value = 4;
    }
    
    
    int main()
    {
        int value;
    
    
        getValue(value);
        cout <<"the value is: " <<value;
    }
    Is there any way i can get the value of value using this function?
    If not what do you suggest i do?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A simple approach is to use a reference parameter:
    Code:
    void getValue(int& value)
    {
        value = 4;
    }
    but for something so simple, this function may or may not be overkill.

    More likely, you would create a class to represent a player, and then an object of this class would store the particular player's hand value. In this way, you can create a container of such player objects.
    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

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You either return a value:
    Code:
    include <iostream>
     
    using namespace std;
     
    int getValue()
    {
        return 4;
    }
     
     
    int main()
    {
        int value;
     
     
        value = getValue();
        cout <<"the value is: " <<value;
    }
    Or you pass the argument into the function by reference:
    Code:
    include <iostream>
     
    using namespace std;
     
    void getValue(int& value)
    {
        value = 4;
    }
     
     
    int main()
    {
        int value;
     
     
        getValue(value);
        cout <<"the value is: " <<value;
    }
    Or as a pointer:
    Code:
    include <iostream>
     
    using namespace std;
     
    void getValue(int * value)
    {
        *value = 4;
    }
     
     
    int main()
    {
        int value;
     
     
        getValue(&value);
        cout <<"the value is: " <<value;
    }
    The reference route seems like the least amount of modification to your code.

    Parameters are passed by value in C/C++. What you give to your getValue function is a copy of the value of what was passed in. Within the getValue function itself, changes made to this copy do not affect the content of the original variable you passed in by in main (having them named the same thing doesn't change this aspect BTW). When the getValue function exits and control returns to main, the variable has the same value as it did before the call to getValue.

    When you pass a pointer, even though what you are passing in is still a copy, it is now a copy of an address, a location that does exist and represents the place in memory of the variable back in the main function. This copy of the address can therefore be used to affect a change to the variable back within the main function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    1
    Yeah ofcourse! You can use pointers.
    Pass the address of the variable as a parameter and then just assign the value of the address to whatever.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by SandDune View Post
    Yeah ofcourse! You can use pointers.
    Pass the address of the variable as a parameter and then just assign the value of the address to whatever.
    No, use references. This is not C...
    For this, references are more than enough. Pointers are more complicated and left for those instance where simple isn't enough.
    Pointers open up a can of worms, so let's not go there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    thanks guys, i'm reading a book on C++ and have not yet learnt about pointers or references yet so I think i'll just use a few global variables for now and fix it up later

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Grae
    i'm reading a book on C++ and have not yet learnt about pointers or references yet so I think i'll just use a few global variables for now and fix it up later
    No, don't do that. If you have not learnt about pointers or references, then follow hk_mp5kpdw's first example. It is critical that you do not develop bad habits at this early stage because bad habits don't matter now, but by the time they do, you would have to work so much harder to get rid of them.
    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

  8. #8
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    ok sure if it is that important ill just leave it until i know how to use pointers and references. I just dot want to use code in my program which i do not yet understand

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you not understand references yet, then you are not ready for your first program, I would say.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variable from an outside function to main?
    By ZeroDivision in forum C Programming
    Replies: 6
    Last Post: 11-08-2013, 08:32 PM
  2. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  3. Can't change variable from another function...
    By Ryan. in forum C Programming
    Replies: 9
    Last Post: 03-11-2011, 12:10 AM
  4. Replies: 4
    Last Post: 02-28-2011, 07:57 AM
  5. how to change it into one main() function code..
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 12-13-2008, 10:02 PM

Tags for this Thread