How can I know the base case of a recursive function please??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I know the base case of a recursive function please???

The base case as a condition to avoid infinite loop

22nd Feb 2020, 3:05 PM
Amal Gil
Amal Gil - avatar
3 Answers
+ 2
You could think about this like: You are writing a recursive function, for example to add all positive numbers up to n. When you call this function initially you will pass the value of n as parameter. You could say the base case is the value that will run every time, no matter how you choose the value of n. So in this example the base case is 1 because it is the smallest positive number. Any n parameter must be 1 or larger, and you decrement it by each recursive call.
22nd Feb 2020, 7:05 PM
Tibor Santa
Tibor Santa - avatar
+ 5
It depends on the problem. It is obvious in most cases, and sometimes they mention it. Can you share the problem?
22nd Feb 2020, 3:07 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
A case that doesen't involve any further calls to that function
13th Jul 2023, 2:36 PM
Mounir Charif
Mounir Charif - avatar