Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 270198d

Browse files
authored
Add files via upload
1 parent c2b62a1 commit 270198d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

recursive_function (1).py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
"""Recursive_function.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1bUQ87PdSkK5DRvQRoL94jEMcy5BizZi_
8+
9+
**NOTE ON RECURSION**
10+
Recursive function:
11+
12+
A recursive function calls itself. This mean that a new function is called from within the original function.
13+
14+
15+
For example look at the code below and assume a linked list counting from 0 to n by 1.
16+
"""
17+
18+
def print_list(head):
19+
if head is not None:
20+
print(head.data)
21+
print_list(head.next)
22+
23+
"""The first function looks at the head of the list and prints it. It then creates a new function to print the next element. However,
24+
first function hasn't ended yet. It can't end until all the functions within it end. This is key to understanding.
25+
26+
If we look at the call stack (the list of running functions) it looks like this:
27+
"""
28+
29+
print_list() # the original outer most function, waiting for internal functions to end.
30+
print_list() # the new function called within the original func.
31+
32+
"""This just keeps on going. The new function calls another function. Now our stack looks like this:"""
33+
34+
print_list() # the original function
35+
print_list() # level 1 function.
36+
print_list() # level 2 function.
37+
38+
"""The call stack keeps getting bigger and bigger because none of the outer functions can terminate because they are waiting on inner
39+
functions.
40+
"""
41+
42+
print_list() # the original function
43+
print_list() # level 1 function.
44+
print_list() # level 2 function.
45+
...
46+
...
47+
...
48+
print_list() # level 1000 - Recursion depth hit. Program ends and error displayed.
49+
50+
"""The call stack keeps getting bigger and bigger because none of the outer functions can terminate because they are waiting on inner
51+
functions.
52+
53+
Recursion depth is a count of how many layers are active inside your recursive function. The default recursion depth limit for
54+
Python 3 is 1000. The limit exists to prevent a stack overflow caused by too much (possibly infinite) recursion.
55+
56+
Recursion is a cool tool and you should use it (or at least understand it). However, some languages have optimized tail call
57+
recursion (what is the kind of recursion being shown above) and avoid this problem by ending the outer function when the inner one is called. Python has not.
58+
59+
If you are writing a function that may hit the recursion limit it may be better to approach it iteratively (at least in Python).
60+
"""

0 commit comments

Comments
 (0)