• April 6th, 2012, 08:11 PM
svo
Can someone please give me a quick code analysis of what is outputted when the method f1(5) is called?
Code java:

``` public in f1( int x )   {   if ( x == 0 )   return 0;   else   return f2( x - 2 );   }   public in f2( int x )   {   if ( x == 1 )   return 1;   else   return f1( x + 1 ) + x;   }```
• April 6th, 2012, 08:14 PM
svo
I got this far:
- Return f2(3) is called
- then in the 3 is passed into the f2(int x) right?
- then it's supposed to do the f1( x + 1) + x .... i'm confused about the " + x" ... how does it work?
• April 6th, 2012, 08:50 PM
Norm
What happens when the code is compiled and executed?
If you want to see the values of intermediate results, add println statements to print them.
• April 6th, 2012, 10:21 PM
svo
D: @-) I'm a nooobb.... soo... and well, I'm supposed to be able to do this in my head... So I was wondering if I could get some help seeing the logic behind what is happening in the code..
• April 6th, 2012, 10:22 PM
svo
I think I sort of see the logic and kinda-sorta understand it up until the last line of "return f1( x + 1 ) + x;" ... :[
• April 6th, 2012, 10:43 PM
lunix
postfix/prefix i think(the order you do the math), if your wanting it to add the x either put it in the brackets that exist or put another set of brackets around the +1. my thinking is that its just returning the value of x along with the result from the function call, as the function take only one argument, unless its in brackets it concatenating(adding) the results together; i.e when all is said and done, we will probably end up with 11, as in x = 1(result from all the function calls), x =1 put together....

5 enters f2 as 3, goes back to 4 and becomes 2 goes up to 3 and finally finishes at 1, essentially taking 2 originally and then going down one at a time.
• April 6th, 2012, 11:16 PM
svo
Oh so I followed the logic in your last sentence and it seems to make sense except i couldn't help but notice that when you went through the "return f1( x + 1 ) + x;", you ignored the "+ x" ? is that allowed? was that on purpose?
• April 6th, 2012, 11:41 PM
helloworld922
No, it cannot be ignored.

f1(x+1) returns some number. Simply substitute that result, perform the +x, and return the result.

Try stepping through the code in debug mode or adding print statements to your code to see how the result as each line of code gets executed.
• April 7th, 2012, 12:20 AM
svo