problem understanding question with function given as multiply(int, int, int&)


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).

http://en.wikipedia.org/wiki/Parameter_%28computer_programming%29#Output_parameters
Last edited on
Hi :) The other integer and float values should be the result of the calculation.For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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.You can find more information about that here http://www.cplusplus.com/doc/tutorial/functions/
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.
Last edited on
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
Topic archived. No new replies allowed.