0% found this document useful (0 votes)
9 views20 pages

Recursion

Recursion is a programming technique where a function calls itself, with a base case that stops the recursion and a general case that continues it. In Python, a recursive function can double an argument until it reaches a maximum recursion depth, with the base case defined as when the argument exceeds 10,000. The concepts of winding and unwinding describe how function calls are managed in the stack during recursion, with winding occurring during the function calls and unwinding happening when the base case is reached.

Uploaded by

zayanbabar420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Recursion

Recursion is a programming technique where a function calls itself, with a base case that stops the recursion and a general case that continues it. In Python, a recursive function can double an argument until it reaches a maximum recursion depth, with the base case defined as when the argument exceeds 10,000. The concepts of winding and unwinding describe how function calls are managed in the stack during recursion, with winding occurring during the function calls and unwinding happening when the base case is reached.

Uploaded by

zayanbabar420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON

9618

RECURSION
What Is Meant By Recursion ?

When a function / procedure calls itself


This is a recursive function in Python that takes an
argument x, doubles it, and then calls itself with the result
as the new argument. The function will keep calling itself
recursively, doubling the argument each time, and printing
out the result, until it encounters an error due to reaching
Python's maximum recursion depth.
The base case and the general case are two important
concepts in recursion that are used to control the flow of
a recursive function.
The base case is the condition that determines when the
recursion should stop. It is the condition that prevents
the function from calling itself again and again, and
allows the function to return a value. In other words, the
base case is the stopping point for the recursion. It is the
case where the problem is simple enough that the
function does not need to call itself anymore.
The general case, on the other hand, is the
opposite of the base case. It is the condition that
determines when the recursion should continue. In
the general case, the function is not yet at the
stopping point and needs to call itself again with a
smaller or simpler version of the problem. This
continues until the base case is reached.
the base case is when the value of y exceeds or
equals 10000, which means that the recursion
stops and the function does not call itself anymore.
The base case is when y >= 10000
The general case is when y < 10000
What Is Meant By Recursive Algorithm ?

Any algorithm that have a base case,


work towards a base case, have a
general case and calls itself.
Concept of winding and unwinding
winding : Is done in general case in which the function calls are
getting pushed in the stack no calculation is done in this phase
Unwinding : When the base case is reached all the data
stored in stack gets popped out from top of the stack
Explain what a compiler has to do to implement recursion?

When the recursive call is made all value are


put on the stack which is known as winding
when the base case is met the algorithm
unwinds. The last set of values are taken off the
stack un reverse order

You might also like