Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

What does the method return: mystery(4,3) public static int mystery(int x, int y){ if(y==1) return x; else return x*mystery(x, y-1); } I watched some videos and read some tutorials online about recursions and I believe I understand, but what I don't understand is x*mystery(x,y-1) What does that line of code even do? How do I times 4 by (4,2)?

OpenStudy (anonymous):

It's a power function. It returns 4^3. Think about it this way. Each time a recursive "instance" is called, it calls the function from the start. For the case mystery(4,3), this is more or less what happens: First call: return 4*mystery(4,2) Second call: return 4*4*mystery(4,1) Third call: return 4*4*4 = 64. The function will be called y times and also return 4 y times. I may be able to write a clearer yet concise explanation of this recursive problem later on, but my brain is malfunctioning right now, my apologies :-)

OpenStudy (anonymous):

you don't multiply 4 by (4,2), you multiply it by mystery(4,2). So next you need to find out what mystery(4,2) returns.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!