Like Tree3Likes
  • 2 Post By Elysia
  • 1 Post By laserlight

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

This is a discussion on Is it possible to change the value of a variable in main using a function? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm currently writing a poker game and am trying my best to avoid using global variables. I have a ...

  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
    22,183
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,806
    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
    Posts
    22,920
    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.
    Elkvis and iMalc like this.
    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
    22,183
    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.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    22,920
    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, 01: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


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