This is a discussion on Introduction to Recursion in OOP within the Programming forums, part of the Tutorials category; Introduction to Recursion in OOP:- A recursive function is a function that calls itself. You have seen instances of functions ...
Introduction to Recursion in OOP:-
A recursive function is a function that calls itself.
You have seen instances of functions calling other functions. In a program, the main function might call function A, which then might call function B. It's also possible for a function to call itself. A function that calls itself is known as a recursive function. For example, look at the message function shown in the Program (given below).
(endless-recursion.py)
# This program has a recursive function.
def main( ):
message ( )
def message ( ):
print 'This is a recursive function. '
message( )
# Call the main function.
main ( )
The message function displays the string 'This is a recursive function' and then calls itself. Each time it calls itself, the cycle is repeated. Can you see a problem with the function? There's no way to stop the recursive calls. This function is like an infinite loop because there is no code to stop it from repeating.
Bookmarks