How wold I pass by reference without using void?

#include <iostream>
using namespace std;

void ADD(const int &num1, const int &num2, float &SUM);
void SUBTRACT(const int &num1, const int &num2, float &Difference);
void MUTIPLY(const int &num1, const int &num2, float &PRODUCT);

int main()

{

int choice = 0;
int total = 0;
float a = 0, b = 0, c = 0;
int num = 0;
int num1, num2;//num1 and num2 go in main
//Menu Chooser
//Choose the type of loop you want to use to average this program



cin >> num1 >> num2;
cout << "OPERATIONS\n\n";
cout << "1 - ADD\n";
cout << "2 - SUBTRACT\n";
cout << "3 - MUTIPLY\n\n";
cout << "4 -Exit\n\n";


cout << "Choice: ";
cin >> choice;

// Demonstrates the switch statement.
//Pick a loop.
{
switch (choice)
{
case 1:
{
//ADDITION
ADD(num1, num2, a);
cout << "\You have chosen to ADD.";
cout << "\The sum is:" << a;

}
break;

case 2:

{

//SUBTRACTION
SUBTRACT(num1, num2, b);
cout << "\You have chosen to SUBTRACT.";
cout << "\The difference is:" << b;
}

break;

case 3:
{
//MUTIPLICATION
MUTIPLY(num1, num2, c);
cout << "\You have chosen to MUTIPLY.";
cout << "\The product is:" << c;

}
break;


case 4:
{
cout << "Goodbye";
}
break;

default:
{
cout << "You made an illegal choice.\n";
}
break;
}

}

return 0;
}
void ADD(const int &num1, const int &num2, float &SUM)

{

//SUM
SUM = num1 + num2;

}

void SUBTRACT(const int &num1, const int &num2, float &Difference)
// SUBTRACT
{
Difference = num1 - num2; //SUBTRACTION // to many equal signs
}


void MUTIPLY(const int &num1, const int &num2, float &PRODUCT)
{

//PRODUCT.
PRODUCT = num1 * num2;// to many equal signs


}
I see some problems with your code but without line numbers it's very hard to tell you where. Please edit your post, highlight the code and click the <> button to the right of the edit window. This will add code tags which will add line numbers and syntax highlighting.

Rather than having a void function that fills in the third parameter, just have the function return the result and assign it in the caller:
1
2
3
4
5
6
7
8
9
10
float ADD(int num1, int num2);
...
//ADDITION
                a = ADD(num1, num2);
...
float
ADD(int num1, int num2)
{
    return num1 + num2;
}


- Since int is a small data type, just pass it by value instead of const &
- Why does ADD() return a float when the sum of 2 ints will always be an int?
- Don't start string constants with "\ unless the first character is an escape sequence like \n or \t.
Ok, so how would this fit into the whole code? Like can you type out thee parts I need to change. Also I had someone help me with this code, as I don't know wha they were doing. All I was asked to do is pass by reference.
Hi,

As presented, you have written 3 functions that each return void and take 3 parameter references.

1. There is no need to declare a const int &. The type qualifier const is sufficient. It indicates to the compiler that the code implementation of the function will not modify the value of the passed argument. Usually, when you pass an argument by-reference, the intent is to allow the called function to modify that argument. Thus using both the "pass-by-reference" operator & and the const qualifier suggests conflicting operations on the argument.

2. Your question isn't clear. Another way to implement ADD/SUB/MULT is by taking in void*. Are you asking how to implement those functions without using void* parameters or without returning void?

3. Please use the <> code format. :)


1. There is no need to declare a const int &. The type qualifier const is sufficient. It indicates to the compiler that the code implementation of the function will not modify the value of the passed argument. Usually, when you pass an argument by-reference, the intent is to allow the called function to modify that argument. Thus using both the "pass-by-reference" operator & and the const qualifier suggests conflicting operations on the argument.

You usually pass by const reference to avoid expensive copy operations caused by passing by value, while not allowing modification of the argument passed. It's recommend to pass small types (char, int, double, etc.) by value, while larger objects (std::vector, std::string, pretty much any STL container) by const reference.
Ok, Thanks I've figured it out!
Ah! I see. I forgot about that. Thank you for the refresher, integralfx.
Topic archived. No new replies allowed.