Python Recursive Functions In Hindi

📔 : Python 🔗

Function को उसी Function के अंदर एक specified condition तक call करने को ही recursive function कहते हैं, और इस process को recursion कहते हैं।

Use Of Recursive Function

  1. Recursive Functions का use tree structure (read या tree structure बनाने में ) में सबसे ज्यादा होता है , जहां पर हमें ये नहीं मालूम होता की Node का कोई children है या नहीं अगर है तो call the function.

  2. दूसरा File Directories को read करने के लिए , क्योंकि हमें नहीं मालूम होता है कि एक directory के अंदर सभी files होगीं या sub directory, और फिर उसके अंदर sub-directories और फिर उसके अंदर etc..

  3. Data Sorting के लिए भी Recursive function use कर सकते हैं।

Python Recursive Function Example

Copy Fullscreen Close Fullscreen Run
num = 0
def print_number(num) : 
  num += 1
  print(num, end=',')
  if(num < 100) :
     print_number(num)

#call the function.
print_number(num)
Output
C:\Users\Rahulkumar\Desktop\python>python recursio_ex.py
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,
63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,
93,94,95,96,97,98,99,100,

ऊपर एक simple recursive function का example दिया गया है , example में 100 बार same function call हुआ है। example को आप ध्यान से देखेंगे तो पायंगे कि एक condition दी हुई है कि agar variable की value 100 से कम है तभी function को call किया गया है।

Note
देखा जाए तो Recursive Functions का work flow same as For Loop की तरह ही होता है , क्योंकि For Loop में भी execution एक specified condition तक repeat होता है। Condition false होने पर Loop end हो जाता था।


Recursive function को use करते समय यह ध्यान रहे कि condition कही न कही गलत जरूर होनी चाहिए otherwise recursion infinity time तक run होता रहेगा जिससे program crack हो सकता।

Python Factorial Example using Recursive Function

Copy Fullscreen Close Fullscreen Run
def find_fact(num) :
  if (num == 0) :
    return 1
  
  print(num, end='*')
  # Recursion.
  result = (num*find_fact(num - 1) )
  return result

print('=', find_fact(5))
print('=', find_fact(3))
Output
C:\Users\Rahulkumar\Desktop\python>python fact_ex.py
5*4*3*2*1*= 120
3*2*1*= 6

Example में simply किसी number का factorial जानने के लिए code implement किया गया है , Example में एक condition दी है कि variable की value 1 से कम होने पर सिर्फ 1 ही return करता है , otherwise same function call होता रहेगा।

Python Disadvantages Of Recursive Function

Recursive Functions , iterative program (like for Loop / while Loop ) के तुलना में काफी memory और time consume करते हैं। इसलिए ज्यादा जरूरी होने पर ही Recursive Functions का use करें।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers