# Count the Number of Recursive Calls in Python

Count the Number of Recursive Calls in Python | In this article, we will learn to count the number of recursive calls in Python programming language.

For counting the number of recursive calls in a Python program. We can use these two methods:-

• By passing a counter with the recursion
• By using Global Variable

## Count the Number of Recursive Calls in Python By Passing a Counter with the Recursion

We can count the number of recursions by just passing a counter in the recursion.

``````def recur(n, count=0):
if n == 0:
return "Finished count %s" % count
return recur(n-1, count+1)

print(recur(5))``````

Output:-

Finished count 5

## Count the Number of Recursive Calls in Python By using Global Variable

We can use another method using a global variable.

``````counter = 0

def recur(n):
global counter
counter += 1
if n == 0:
return -1
else:
return recur(n-1)

print(recur(100))
print("Number of Recursive Calls: ", counter)``````

Output:-

-1
Number of Recursive Calls: 101

So, here, in this article, we have discussed how to count the number of recursions or recursive calls in Python programming language. Hope you all enjoy the post and find it useful.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!