Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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)
C#
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.
 
Share this answer
 
Comments
Member 14517556 6-Jul-19 5:00am    
Thank you. Very beautifully explained
OriginalGriff 6-Jul-19 5:08am    
You're welcome!
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.
 
Share this answer
 
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
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900