-4
#include <iostream>
#include <iomanip>

using namespace std; 

void getSales(int sales);

int main()
{
    //declare variables
    int sales = 0;
    double commission = 0.0;

    //enter input 
    getSales(sales);

    //determine comission
    if (sales < 0)
        commission = -1;
    else if (sales <= 100000)
        commission = sales * .02;
    else if (sales <= 400000)
        commission = (sales - 100000) * .05 + 2000;
    else
        commission = (sales - 400000) * .1 + 17000;

    //display commission error message
    if (commission != -1)
    {
        cout << fixed << setprecision(2);
        cout << "Comission: $" << commission << endl;
    }
    else
        cout << "The sales cannot be less than 0." << endl;

    return 0;
}

//pass variable by reference
void getSales(int sales)
{
    cout << "Sales: ";
    cin >> sales;
}

Alright, so I've been trying to understand about the difference between passing a variable by value and by reference. In this case I'm trying to just do a simple pass by reference with a void getSales, but my program won't actually call in my algorithm. So every time I input the sales it won't print the result with my algorithm. I'm not very experienced with passing values so any help would be appreciated so I know what I'm missing here.

Thanks a bunch!

starrynights89
  • 119
  • 1
  • 3
  • 10
  • 1
    You should check out [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and find a beginners book or tutorial, because this issue will be found very easy that way with some nice explanation that tells the difference. – Some programmer dude Feb 14 '16 at 18:53
  • http://stackoverflow.com/questions/2564873/how-do-i-use-reference-parameters-in-c – JavaSheriff Feb 14 '16 at 18:54
  • This is a pretty convoluted way of trying to figure out the difference between pass by value and pass by reference. You should simplify your example. – juanchopanza Feb 14 '16 at 18:55
  • your getSales() argument not pass by reference it's call pass by value. – Prosen Ghosh Feb 14 '16 at 18:58

4 Answers4

0

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function.

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call.

void getSales(int sales)
{
    //call by value function
}

void getSales(int &sales)
{
    //call by reference function
}
Prosen Ghosh
  • 635
  • 7
  • 16
0
//pass variable by reference
void getSales(int sales)
{
}

Doesn't pass the parameter by reference but by value.

To pass it by reference use the reference specifier for the type:

void getSales(int& sales)
              // ^
{
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

Passing by reference is similar to passing by pointer (except that references can't be re-seated), meaning that the function you call don't get a copy of the passed value but accesses the one passed in from the callers scope. To pass by reference you must add an ampersand '&' to the type in your getSales function. As it stands now you are passing the int by value whereas 'int&' would pass by reference. Just like 'int*' would pass by pointer.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

I guess your question has already been answered, but why would you want to return the value as an argument of a void function and not simply declare

int getSales();

I think that output arguments should only be used as a last resort. They tend to make code hard to read.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45