Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.10: Recursion

0 Replies, 944 Views

Recursion / Recursive functions:
         In this thread we are going to have a deep look at the recursive approach. It is a concept in which a function calls itself inside of it. The function is called a recursive function. We use base case to terminate the calling if a required condition is met otherwise without a base case the program will throw an error. The most simple example of a recursion function is the factorial program. If you know what is factorial well and good if you don’t so first look at it mathematically for example we want to check the factorial of 5 what we will do we will multiply all positive integers below that number. In mathematics, the factorial is represented by ‘!’

Quote:5!  = 5 ✕ 4 ✕ 3 ✕ 2 ✕ 1


So in this way we can check the factorial of any number. Let's code it for understanding recursive functions.

Code:
def facto(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * facto(n-1)

n = int(input("Enter a number: "))
print(facto(n))


Let’s understand the code that we did in the first step. We gave it a base case if the number is 0 or 1 return 1 because the factorial of 1 and 0 is always 1. But in else case there is logic to understand
Firstly it will take number which is greater than 1 suppose the number is 5 so it will multiply the number just like this
5*facto(4)
In second step
5*4*facto(3)
And so on until it reaches the base case and at this step the recursive stack will become like this 5*4*3*2*1.
So this was a simple example of recursive functions.

Now let’s code another example which is the fibonacci series if you don’t know this is it.

PHP Code:
0 1 1 2 3 5 8 13 … 


Now we will make a program and it will take index of number as input and return that number.

Code:
def fib_series(n):
    if n == 1:
        return 0
    elif n== 2:
        return 1
    else:
        return fib_series(n-1) + fib_series(n-2)

n = int(input("Enter a number: "))
print(fib_series(n))


Hope you have understand this problem of recursion

So the problem is which one to use iterative or recursive method it is difficult to trace an recursive method which I do not suggest for complex and large project for large programs the iterative method will take time for writing the code but it will be easy to debugging and recursive method is good for simple programs.

That’s it for now till then 

"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,249 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,638 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,673 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,843 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,607 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,841 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,792 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,833 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,825 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,827 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)