Consider the following code fragment
public int fun(int a, int b) {
if (b == 1)
return a;
else
return a + fun(a, b - 1);
}
a. Draw the decomposition & composition of fun(2,4)
b. What would be the value of fun(2,0)?Discuss and show a screenshot that proves your answer.
Your question sounds like a homework task and I don't want to do the work for you.
That said, I can draw the decomposition/composition diagram for another example to give you an idea.
Here's a diagram for
fun(0,3):where black arrows indicate the decomposition and blue arrows the composition of
fun(0,3).The decomposition procedure (black):
Draw
fun(0, 3)By examining your code, you can see that in order to compute
fun(0, 3), you first have to computefun(0, 2), so you drawfun(0, 2)In order to compute
fun(0, 2), you first have to computefun(0, 1), so you drawfun(0, 1)The composition procedure (blue):
fun(0, 1)returnsa, or in your case0You can now compute
fun(0, 2) = a + fun(0, 1) = 0 + 0 = 0And finally, you can compute
fun(0, 3) = a + fun(0, 2) = 0 + 0 = 0As you've seen, that particular case produces a
StackOverflowerror. If you tried drawing a decomposition/composition diagram for that case, you'd end up with a single, endless branch.