Help please :(


#include <iostream>

using namespace std;

int fun (int a, int b=2)
{

return 3*a + 2*b +1;

}

float fun (float a, float b = 2.0)
{

return 3.0*a + 2.0*b + 1.0;
}


int main ()
{

cout << "Enter a number: ";
float x;
cin >> x;
cout << "Result: " << fun(x) << endl;
return 0;


}

a) Which "fun" will be executed?

b) what is result, if 2 was entered?

c) How is "x" passed (by value, by reference, by name)?
Please use code tags:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int fun (int a, int b=2)
{

return 3*a + 2*b +1;

}

float fun (float a, float b = 2.0)
{

return 3.0*a + 2.0*b + 1.0;
}


int main ()
{

cout << "Enter a number: ";
float x;
cin >> x;
cout << "Result: " << fun(x) << endl;
return 0;


}


a)
 
float fun (float a, float b = 2.0)

Will be executed because you supply it with a float parameter. The other parameter is a default, so it is optional. Make sense?
b)
11.0
Please work this out for yourself.
c)
In C++, primitives(int, float, long) are passed by value.


Hope this helps and good luck!
theturk1234
Thanks! For question b, it would be eleven because you would substitute a with 2.
So 3(2) + 2(2) +1 = 11 right?
Perfect!
Topic archived. No new replies allowed.