C++ Recursive Function In Hindi

📔 : C++ 🔗

PHP , JavaScript, C की तरह ही C++ में भी हम recursive functions define कर सकते है।


Same Function को उसी Function के अंदर call करने को ही Recursive Function कहते हैं , और इस process को Recursion कहते हैं।

C++ Need Of Recursive Function

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

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

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

C++ Recursive Function Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
void print_number(int number)
{
  // print number.
  cout << number << " , ";
  // increment by 1.
  number++;
  // if number is less than 100 then call the function.
  if(number <= 100)
  {
    // call the function.
    print_number(number);
  }
}
int main() {
  print_number(1);
  return 0;
}
Output
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 हो जाता था।


C++ Factorial Example using Recursive Function
CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int fact(int number)
{
  if(number <= 1)
    return 1;
  else
    return number*fact(number-1);
}
int main() {
  cout << "Factorial of 4 : " << fact(4) << endl;
  cout << "Factorial of 5 : " << fact(5) << endl;
  cout << "Factorial of 8 : " << fact(8);
  return 0;
}
Output
Factorial of 4 : 24
Factorial of 5 : 120
Factorial of 8 : 40320

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


Recursive Function में Loop की तरह ही condition देना mandatory है , otherwise function infinite call करता रहेगा और page breach हो सकता है।

C++ 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