In: Computer Science
C++: Write the 2 possible ways the compiler could make function calls for the following operation. If there is a reason why there should only be one way to call this function, then explain why. The num variables are instances of the Number class.
fout << num;
#include <iostream> using namespace std; // function declaration int max(int num1, int num2); int main () { // local variable declaration: int a = 100; int b = 200; int ret; // calling a function to get max value. ret = max(a, b); cout << "Max value is : " << ret << endl; return 0; } // function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; }
the arguments passed to the functions have been passed by
value. This means that when calling a function with
parameters, what we have passed to the function were copies of
their values but never the variables themselves. For example,
suppose that we called our first function addition using the
following code:
int x = 3, y = 3 , z;
z = addition(5,3);
What we did in this case was to call to function addition passing
the values of x and y, i.e. 5 and 3 respectively, but not the
variables x and y themselves.
int addition (int a, int b);
z=addition(5,3);
This way, when the function addition is called, the value of its
local variables a and b become 5 and 3 respectively, but any
modification to either a or b within the function addition will not
have any effect in the values of x and y outside it, because
variables x and y were not themselves passed to the function, but
only copies of their values at the moment the function was
called.