0

I'm a freshman in cs, and I am having a little issue understanding the content of recursion in python.

I have 3 questions that I want to ask, or to confirm if I am understanding the content correctly.

1: I'm not sure the purpose of using base case in recursion. Is base case worked to terminate the recursion somewhere in my program?

2: Suppose I have my recursive call above any other code of my program. Is the program going to run the recursion first and then to run the content after recursion?

3: How to trace recursion properly with a reasonable correctness rate? I personally think it's really hard to trace recursion and I can barely find some instruction online.

Thanks for answering my questions.

1
  • 1
    1. The base case is the terminal case for the recursion. 2. Yes, the recursive call has to terminate and fully unwind the stack before continuing. 3. Depth in the call stack is often useful for debugging, you can add an extra parameter which holds the depth for diagnostic printing. Commented Aug 14, 2017 at 1:52

1 Answer 1

1
  1. Yes. The idea is that the recursive call will continue to call itself (with different inputs) until it calls the base case (or one of many base cases). The base case is usually a case of your problem where the solution is trivial and doesn't rely on any other parts of the problem. It then walks through the calls backwards, building on the answer it got for the simplest version of the question.

  2. Yes. Interacting with recursive functions from the outside is exactly the same as interacting with any other function.

  3. Depends on what you're writing, and how comfortable you are with debugging tools. If you're trying to track what values are getting passed back and forth between recursive calls, printing all the parameters at the start of the function and the return value at the end can take you pretty far.

The easiest way to wrap your head around this stuff is to write some of your own. Start with some basic examples (the classic is the factorial function) and ask for help if you get stuck.

Edit: If you're more math-oriented, you might look up mathematical induction (you will learn it anyway as part of cs education). It's the exact same concept, just taught a little differently.

Sign up to request clarification or add additional context in comments.

2 Comments

I know this question is for Python, however, if you know one language you know all of them. The way I learned recursion is this site, codingbat.com/java/Recursion-1. You could even just write the problems there in Python and learn some unit testing :)
@JohnHamlettIV That's a good observation that I didn't mention in my answer. Recursion is language agnostic, and anyone trying to learn about it should be aware of that. (I'll take the python tag off of this question, you're right that it doesn't belong)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.