View Single Post
  #1 (permalink)  
Old 04-23-2008, 08:31 AM
sivasayanth's Avatar
sivasayanth sivasayanth is offline
Member
 
Join Date: Dec 2007
Posts: 20
sivasayanth is on a distinguished road
i could not get the recursion in java
I have just studied one java source code. That entirely belongs to recursion in java. However, I could not understand what happened in the execution sequence.
Code:
public class MainClass { public static void main(String[] args) { double x = 5.0; System.out.println(x + " to the power 4 is " + power(x, 4)); System.out.println("7.5 to the power 5 is " + power(7.5, 5)); System.out.println("7.5 to the power 0 is " + power(7.5, 0)); System.out.println("10 to the power -2 is " + power(10, -2)); } // Raise x to the power n static double power(double x, int n) { if (n > 1) return x * power(x, n - 1); // Recursive call else if (n < 0) return 1.0 / power(x, -n); // Negative power of x else return x; } }
Please help me.
What is the return value When first execution happened?
Reply With Quote
Sponsored Links