About how functions break in recursion

What happens

Hi everyone, i was trying to solve a challenge called two printers, at the beggining i solve it without functional programming using loops like for and foreach, now i pass it to functions and i see that the program doesnt return nothing on the terminal, but is because when the function call itself a lot of times just break and dont end with the process

What do you understand or find about that problem

i cant find nothing on internet about why the function just break call itself when she repeat a lot of times the action

Did you try any workaround? What did you do?

yes, i made two test programs, in one i made a for loop that prints a number secuence (1,2,3,4…) in other i do the same test but with functions and its break and dont finish the returns


here is the output of the test with functions, see that doesnt end until 1

and here the test with the for loop that ends correctly :

I need help with

how can i make a function that call itself a lot of times like a million thousand of times

When using recursion, you must always make sure the function ends in some way, like a limit using some variable, this is called a base case.
If you want to understand it better, I encourage you to read this article.

Hi, i put the base case to ensure, that it isnt a infinite call, but the error is related to the memory of the function.

Ok, I understand.
When the error says something about the memory in a recursive function is usually a stack overflow, the most common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.
You could try using memoization, to optimize the use of memory in the function