You are on page 1of 1

Dear Student,

Please read the code in sequence from 1 to 8. I have annotated the code with value of “n” and
expression results

result = factR(n-1 (3)) means that value “3” is sent to factR function.

int factR(int n(4)) { means that “4” was received.

So, please read it and let me know if you are still not clear.

4: 5:

int factR(int n (1)) { int factR(int n ) {


int result; int result;
(1) if(n==1) return 1; (1) if(n==1) return 1; //1
(2) result = factR(n-1) * n; (2) result = factR(n-1) * n;
(3) System.out.println(result); (3) System.out.println(result);
(4) return result; (4)return result;
} }

3: 6:

int factR(int n (2)) { int factR(int n ) {


int result; int result;
(1) if(n==1) return 1; (1) if(n==1) return 1;
(2) result = factR(n-1 (1)) * n; (2) result = factR(n-1) * n; //result = 2 (factR returned 1 & n was 2)
 (3)   System.out.println(result); (3) System.out.println(result); // 2 will be output
(4) return result; (4) return result; // 2 will be returned
} }

2: 7:

int factR(int n (3)) { int factR(int n ) {


int result; int result;
(1) if(n==1) return 1; (1) if(n==1) return 1; //1
(2) result = factR(n-1 (2)) * n; (2) result = factR(n-1) * n; //result = 6 (factR returned 2 and n was 3)
(3)  System.out.println(result); (3) System.out.println(result); // 6 will be output
(4) return result; (4) return result; // 6 will be returned
} }

1: 8:

int factR(int n(4)) { nt factR(int n ) {


int result; int result;
(1) if(n==1) return 1; (1) if(n==1) return 1; //1
(2) result = factR(n-1 (3)) * n; (2) result = factR(n-1) * n; //result = 24 (factR returned 6 and n was 4)
(3)  System.out.println(result); (3) System.out.println(result); // 24 will be output
(4) return result; (4) return result; // 24 will be returned.
}
}

You might also like