Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

How does a recursion stop? If it keeps calling itself, when does it finally end?

OpenStudy (anonymous):

never ends if no break and return condition is set , untill stack overflows,

OpenStudy (anonymous):

I couldn't get you, can you make it more clear?

OpenStudy (anonymous):

when a function calls itself or another function the address of next line to be executed in memory will be stored on a data structure named stack , cause stack has a limit for saving these , when it call itself without any condition its like a always true condition for a a while , like , while(true) { commands here } , when it exceeds stack limit size for program , then occurs an error , named stackoverflow

OpenStudy (anonymous):

why would anyone use a recursion without any condition? Is there any purpose behind it?

OpenStudy (anonymous):

consider this example , myFunc() { myFunc(); anotherFunc(); } as you see , anotherFunc() has no chance to be executed , its just an error . i mean a Fatal error

OpenStudy (anonymous):

you have to tell it when to stop - give it a 'base case'. in the following example the base case is when the argument is equal to '' http://pastebin.com/5GUJ2Aum

OpenStudy (anonymous):

so,it keeps on calling until any condition is fulfilled? For this case, until the string is empty(blank)?

OpenStudy (anonymous):

And what does s[:-1] mean?

OpenStudy (anonymous):

i haven't experience with Python , if that's a python code

OpenStudy (anonymous):

this is a forum for a course that uses python to learn C.S. that is a slice, it is in the Python tutorial and some of the early readings

OpenStudy (anonymous):

And what does s[:-1] mean? this will return all of the string 's' from the first character to the final character -1. s = 'foo' print s[:-1] >>>fo

OpenStudy (anonymous):

How does a recursion stop? If it keeps calling itself, when does it finally end? you must put a test in the code so that the recursion is stopped. i.e looop test true, or call a return. otherwise infinite loop.

OpenStudy (anonymous):

as Jeffrey says above you do need an exit hatch to stop the recursion. one approach is to put to a condition (e.g. an if statement). The following code calculates the factorial of an integer using recursion. Notice in the case of the code above, the recursion stops when the condition n==1 is true.

OpenStudy (anonymous):

forgot to post the code. here it is: def factorial(n): if n==1: return 1 else: return factorial(n-1)*n x = int(raw_input("Please enter an integer: ")) print factorial(x)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!