0

I know that recursion has something to do with calling a function inside of a function, but I don't know how to code it. Since it's "calling a function inside of itself", I tried this:

#recursion function def recursion_function(): print("this is a function") recursion_function() 

And there was no output in the console. How do I fix this? Please help. Thanks.

2
  • 1
    You need to call the function initially, then it'll loop. Commented Apr 2, 2018 at 0:52
  • I don't really get what you mean by calling the function "initially"; can you please explain? Thanks. Commented Apr 2, 2018 at 0:54

2 Answers 2

1

As S. Dev previously answered, you need to call the function. The code you posted was only the function implementation.

Also as it is right now, your recursive function will run indefinitely because you don't have any kind of exit condition, so probably you'll want to do something similar to this instead.

#recursion function def recursion_function(n): if(n > 0): print("this is a function") recursion_function(n-1) recursion_function(5) 

Now this way, you have an exit condition and you won't be getting an error.

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

Comments

1

You need to call the function once in order to trigger the loop.

def recursion_function(): print("this is a function") recursion_function() recursion_function() 

Python will eventually trigger an RecursionError though, if its an open-ended loop.

2 Comments

how can I make this a close-ended loop, then?
Something within recursion_function needs to break the loop with either the break keyword, or by returning before the next execution of the loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.