+ 1
What is the use calling the method inside the method?
example, the nethod is "static int num(int a)" inside method is "result = num( a - 1) * n ; ". sorry bad english i am Indonesian people
2 odpowiedzi
+ 2
Calling the same method inside a method is called as recursion.
+ 2
To implement a recursive algorithm, the problem must satisfy the following conditions:
It can be broken down into a simpler version of the same problem
It has one or more base cases whose values are known
My favorite real world example is this one:
Let's pretend that you're seated in the last row of a large, packed classroom.
Now, for some inexplicable reason, you want to know the number of rows in the room. You're too lazy to count so you ask Mike who is seated in front of you to tell you how many rows are there in front of him. Mike does the same thing and asks Patil who is seated in front of him and so on till Alice who is seated first row is asked the question. Alice can answer Jack seated in the second row because she can see that there are no more rows in front of her. This is the base case. Jack adds 1 to Alice's answer and tells it to Jill who is in the third row and so on until the answer reaches you. Notice that at each step the problem is reduced to a simpler version of the same problem.