Q)Write a C++ Program that contains four user defined function(s) plus(int, int, int&),
minus(int, int, int&), multiply(int, int, int&), divide(int, int, float&).
In main() function:
o Get two numbers from user
o Call four user defined fuctions
o Print the result from plus(), minus(), multiply(), divide().
In user defined functions:
o Plus and Minus function get two interger values.
o Multiply and Divide functions get two interge values.
we've been asked to get 2 values so why is there another integer and float value in both functions with address operator
My guess is that the result of the computation should be assigned to the variable that was passed as third argument to the function, instead of using the return value (The function return type should probably be void).
#include<iostream>
void plus(int, int, int&);
int main()
{
int a = 3;
int b = 7;
int result;
plus(a, b, result);
std::cout << a << " + " << b << " = " << result << std::endl;
system("pause");
return 0;
}
void plus(int a, int b, int& result)
{
result = a + b;
}
The address operator is used for pass by reference.
No, & in your program is not an operator and it has nothing to do with the address-of operator. C++ just happen to use the same symbol in different context to mean different things.
Thanks guys for the replies, indeed the reason of the third parameter is to assign the result of a and b to the third one.
Thanks for help everyone and especially konstance for the example