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
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.
As presented, you have written 3 functions that each return void and take 3 parameter references.
1. There is no need to declare a constint &. 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?
1. There is no need to declare a constint &. 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.