Confused on Pass By Reference

This is a discussion on Confused on Pass By Reference within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that calculates the number of soccer teams and players that will be available. ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    4

    Confused on Pass By Reference

    I am trying to write a program that calculates the number of soccer teams and players that will be available. I am very confused as to which parameters need to be pass by reference. I've looked at the definition and I don't understand what it means. My code is probably wrong in the functions, but hopefully I can figure those out after I get the pass-by-references right.

    Code:
    TODO:   Rewrite this program using the following 4 functions.
    
                (1) An input function for getting the number of players per team.
    
    
                (2) Another input function for getting the number of players
                    available.
    
    
                (3) A function to calculate the number of teams and number of
                    left-over players.
    
    
                (4) An output function to print the result.
    
    
        NOTE:   Use and document the appropriate parameter passing mechanism
                for each parameter: in, out, inout
    
    
                In the end, the main() function should include just the variable
                declarations, 4 function call statements, and the return statement.
    */
    
    
    #include <iostream>
    using namespace std;
    
    
    
    
    
    
    
    
    int main()
    {
       int players,      // Number of available players
           teamPlayers,  // Number of desired players per team
           numTeams,    // Number of teams
           leftOver;     // Number of players left over
    
    
       void getPlayers(int&);
       void playersPerTeam(int&);
       void numOfTeams(int&, int&, int&);
       void displayResults(int, int, int, int);
    
    
    
    
       getPlayers(players);
       playersPerTeam(teamPlayers);
       numOfTeams(numTeams, teamPlayers, players);
       displayResults(numTeams, leftOver);
    
    return 0;
    }
    
     void getPlayers(int& players)
       {
           cout << "How many players are available? ";
           cin >> players;
    
    
       // Validate the input using a while loop.
       while (players <= 0)
            {
                cout << "Please enter a positive number: ";
                cin >> players;
            }
       }
    
    
    
    
    
    
        void playersPerTeam(int& teamPlayers)
        {
       // Validate the input using a while loop.
            while (teamPlayers < 9 || teamPlayers > 15)
                {
                     cout << "You should have at least 9 but no\n";
                     cout << "more than 15 per team.\n";
                     cout << "How many players do you wish per team? ";
                     cin >> teamPlayers;
                }
    
    
              cout << endl;
        }
    
    
        void numOfTeams(int& numTeams, int& teamPlayers, int& players)
        {
            numTeams = players / teamPlayers;
        }
    
    
        void displayResults(int players, int leftOver, int teamPlayers, int numTeams)
        {
            leftOver = players % teamPlayers;
            cout << "There will be " << numTeams << " teams with ";
            cout << leftOver << " players left over.\n";
        }
    Last edited by Reynolds773; 10-30-2012 at 03:13 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    Edit: I thought this was in C not C++; so, it might be off since I am just learning C++.

    The term pass-by-references means it is being past by address/pointer instead of by value.
    Use pass by value when the value in the calling function is NOT supposed to change when the called function runs.
    Use pass by address if the value in the calling function is supposed to be changed when the called function runs.

    Edit: C++ has pass-by-reference added to it; C does not really have it.
    The main difference is syntax, and references can not or should not be NULL, I am a C programmers so there maybe more differences.
    The syntax difference is the lack of a indirection "*" needed in C; but references do not need them.

    If only a single value needs to be effected by the called function, it is more common to return the value from the function.

    NOTE: In C, functions can NOT return arrays; but, can return structures.

    Tim S.
    Last edited by stahta01; 10-30-2012 at 03:25 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Okay, I think I'm understanding it a little more. I got the program to work. Now I can study off of it. Thanks!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    Quote Originally Posted by Reynolds773 View Post
    Okay, I think I'm understanding it a little more. I got the program to work. Now I can study off of it. Thanks!
    I forgot another C++ difference, passing by reference is faster. But, if the object being passed is not supposed to be changed you need to add a const keyword somewhere. In C, pointers are sometimes used for large data objects for the same speed reason.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    I forgot another C++ difference, passing by reference is faster. But, if the object being passed is not supposed to be changed you need to add a const keyword somewhere. In C, pointers are sometimes used for large data objects for the same speed reason.

    Tim S.
    I'll add that to my notes, thanks!

  6. #6
    Registered User meb193's Avatar
    Join Date
    Oct 2012
    Posts
    2

    Confused as well

    Quote Originally Posted by stahta01 View Post
    I forgot another C++ difference, passing by reference is faster. But, if the object being passed is not supposed to be changed you need to add a const keyword somewhere. In C, pointers are sometimes used for large data objects for the same speed reason.

    Tim S.
    I've the same program as my homework assignment (and everything is the same except maybe some of the prompt text) and I'm currently experiencing an error that says the variable leftOver is being used without being initialized..I'm not sure how to correct this
    Last edited by meb193; 10-30-2012 at 08:03 PM. Reason: Realized first mistake

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Post your code! Maybe we can help you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with pointers and pass by reference
    By 50tonrobot in forum C Programming
    Replies: 16
    Last Post: 07-12-2012, 01:05 AM
  2. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  3. Confused as to how to pass back to main
    By J-Camz in forum C Programming
    Replies: 6
    Last Post: 11-28-2008, 07:21 AM
  4. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 02:03 PM

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