Writing and Calling Functions Part A True or False A functio
Writing and Calling Functions
Part A
True or False?
A function has exactly one return statement.
A function has at least one return statement.
A function has at most once return value.
A procedure (with return value void) never has a return statement.
When executing a return statement, the functions exists immediately.
A function without parameters always has sideeffects.
A procedure (with a return value void) always has a side effect.
A function without side effects always returns the same value when called with the same parameter values.
Part B
Consider these functions:
Double F(double x) { return g(x) + sqrt(h(x)); }
Double G(double x) { return 4 * h(x); }
Double H(double x) { return x * x + k(x) – 1; }
Double K(double x) { return2 * (x + 1); }
Without actually compiling and running a program, determine the results of the following function calls:
Double x1 = F(2);
Double x2 = G(H(2));
Double x3 = K(G(2) + H(2));
Double x4 = F(0) + F(1) + F(2);
Double x5 = F(-1) + G(-1) + H(-1) + K(-1);
Part C
Write a procedure sort3(int& a, int& b, int& c) that swaps its three inputs to arrange them in sorted order.
For example:
int v = 3;
int w = 4;
int x = 1;
sort3(v, w, x); //V is now 1, w is not 3, x is now 4
Solution
PART A:
1. A function has exactly only one return statement.
Above statement is false
2. A function has at least one return statement.
Above statement is false;
3. A function has at most return one value.
Above statement is true;
4. A procedure (with return value void) never has a return statement.
Above statement is false;
5. When executing return statement function exits immediately.
Above statement is true;
6. A function without parameters always has side-effects.
Above statement is false
7. A procedure with return value of void always has side-effect.
Above statement is false
8. A function without side effects always returns same value with same parameter values.
Above statement is true
PART B:
Consider these functions:
double F(double x) {return G(x) + sqrt (H(x)); }
double G(double x) {return 4*H(x); }
double H(double x) {return x*x+ K(x)-1; }
double K(double x) {return 2*(x+1); }
1. double x1 = F(2) ;
= G(2) + sqrt (H(2))
= 4*(H(2) + sqrt(H(2)
= 4*9+sqrt(9)
= 36+3
x1 = 39
Where H(2) = 2*2+K(2)-1
= 4+2(2+1)-1
= 4+4+2-1 = 9
2. double x2 = G(H(2));
= G(9)
= 4*H(9)
= 4*100
x2= 400
Where H(9) = 9*9+K(9)-1
= 81+2(9+1)-1
= 81+2*9+1
= 81+18+1 = 100
3. double x3= K(G(2)+H(2));
G(2) = 4*H(2)
= 4*9 = 36
H(2) = 9
K(36+9) = K(45)
= 2*(45+1)
= 2*46
x3= 92
4. double x4 = F(0) + F(1) + F(2);
F(0) = G(0) + sqrt(H(0))
= 4*H(0) + sqrt(H(0))
= 4*(0+1)^2 + 0+1
= 4*1+1 = 5
F(1) = G(1) + sqrt(H(1))
= 4*H(1) + sqrt(H(1))
= 4*(1+1)^2 + 1+1
= 4*4+2 = 18
F(2) = G(2) + sqrt(H(2))
= 4*H(2) + sqrt(H(2))
= 4*(2+1)^2 + 2+1
= 4*9+3 = 39
x4= 5+18+39 = 62
5. double x5 = F(-1) + G(-1) + H(-1) + K(-1);
F(0) = G(-1) + sqrt(H(-1))
= 4*H(-1) + sqrt(H(-1))
= 4*(-1+1)^2 + -1+1
= 4*0+0 = 0
G(-1) = 4*H(-1)
= 4*(-1+1)^2 = 4*0 = 0
H(-1) = (-1+1)^2= 0
K(-1) = 2*(-1+1) = 0
x5 = 0+0+0+0 = 0
PART C:
void sort3(int& a,int& b, int& c)
{
if(a>b) { int temp =a; a=b; b= temp; }
if(b>c) { int temp =b; b=c; c= temp; }
if(a>b) { int temp =a; a=b; b= temp; }
}



