Click here to Skip to main content
14,327,288 members
Rate this:
Please Sign up or sign in to vote.
Can some body please explain the following lines in detail:

A correct simple recursive function definition is based on four key concepts:
1. The function optionally must call itself within its definition; this is the recursive case.
2. The function optionally must not call itself within its definition; this is the base case.
3. Some sort of conditional execution (such as an if/else statement) selects between the recursive case
and the base case based on one or more parameters passed to the function.
4. Each invocation that does correspond to the base case must call itself with parameter(s) that move the
execution closer to the base case. The function’s recursive execution must converge to the base case.

What I have tried:

I tried to understand this but was not successful. i can't understand that what is calling function within itself? What is the base case and recursive case?


if some one have some theory related to this please forward me or recommend me some book or video from which this could be understood.
Posted
Updated 6-Jul-19 0:18am
Rate this:
Please Sign up or sign in to vote.

Solution 2

Calling a function within itself is simple. Think of the classic example: factorials.
n! is n * (n-1) * (n-2) * (n-3) until the terms reach 1. E.g.:
4! = 4 * 3 * 2 * 1
6! = 6 * 5 * 4 * 3 * 2 * 1
In C# code (I refuse to write Python, it's a toy language)
int Factorial(int n)
   {
   if (n <= 1) return 1;
   return n * (Factorial(n - 1);
   }
The function Factorial calls itself to generate the sequence. Each time it is called, it is passed a parameter which is one lower than it was lasts time, until it reaches the end, when it returns 1 instead.
Returning 1 is the "base case", the rest is the "recursive case".

Try it on paper, and see what happens. It's pretty obvious when you get your head around it.
   
Comments
Member 14517556 6-Jul-19 5:00am
   
Thank you. Very beautifully explained
OriginalGriff 6-Jul-19 5:08am
   
You're welcome!
Rate this:
Please Sign up or sign in to vote.

Solution 3

Part of your problem may be that the 'concepts' you state have a slight flaw …

4. Each invocation that does correspond to the base case must call itself with parameter(s) that move the execution closer to the base case. The function’s recursive execution must converge to the base case.

should be

4. Each invocation that does not correspond to the base case must call itself with parameter(s) that move the execution closer to the base case. The function’s recursive execution must converge to the base case.
   
Comments
Member 14517556 6-Jul-19 10:54am
   
i have taken this theory from a book so its not my text but thank you for your answer
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
I tried to understand this but was not successful. i can't understand that what is calling function within itself? What is the base case and recursive case?

Remember: Google is your friend.
Have some lecture:
Recursion - Wikipedia[^]
Recursion (computer science) - Wikipedia[^]
Recursion - GeeksforGeeks[^]

Once you found some sample recursive code, use the debugger to watch the code performing, the debugger is a great learning tool.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100