Share
Explore BrainMass

Output of recursion in C++

Consider the following function:

int Func(int x)
{
if(x == 0)
return 2;
else if(x == 1)
return 3;
else
return (Func(x - 1) + Func(x - 2));
}

What is the output of the following statements?
a. cout<<Func(0)<<endl;
b. cout<<Func(1)<<endl;
c. cout<<Func(2)<<endl;
d. cout<<Func(5)<<endl;

Solution Preview

Please see the attached file.

a. Calling func() with x=0.
Thus, The first condition of x=0 is satisfied. Function will return 0.
Output would be: 2

b. cout<<Func(1)<<endl;
Thus, The Second condition of x=1 is satisfied. Function will return 0.
Output would be: ...

Solution Summary

This posting contains output to following program in C++.

$2.19