← Back to C Course | Chapter 5: Functions | Lesson 5 of 8

C में Recursion

Recursion तब होता है जब एक function एक बड़ी problem को इसके एक छोटे हिस्से पर खुद को call करके solve करता है, अंदर छोटी dolls वाली nesting dolls की तरह। यह सबसे छोटी doll पर रुकता है।
Syntax
c
return_type function_name(parameters) {
  if (base_case_condition) {
    return base_value;
  }
  return function_name(smaller_input);  // recursive case
}

Recursion क्या है?

Recursion तब होता है जब एक function खुद को, directly या indirectly, call करता है, आपको एक solution को same problem के एक छोटे version की तरह express करने देते हुए -- उन tasks के लिए useful जो naturally repeated, self-similar steps में टूटते हैं।

उदाहरण: What is Recursion?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void countdown(int n) {
	if (n == 0) return;
	// Print formatted text to the screen
	printf("%d ", n);
	countdown(n - 1);
}
// Program execution starts in main()
int main() {
	countdown(3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base Case

हर recursive function को एक base case चाहिए -- एक condition जहाँ यह खुद को call करना बंद कर देता है और बस एक value return करता है -- इसके बिना, हर call तब तक अगली call spawn करता रहता है जब तक program crash न हो जाए।

उदाहरण: The Base Case

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int factorial(int n) {
	// Check whether n == 0
	if (n == 0) {
		// Send 1 back to the caller
		return 1;
	}
	// Send n * factorial(n - 1) back to the caller
	return n * factorial(n - 1);
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", factorial(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Recursive Factorial

एक factorial recursively compute करना N! को N गुणा (N-1)! की तरह express करता है, base case के साथ कि 0! 1 के बराबर है -- हर recursive call पिछले से एक step छोटा handle करता है जब तक यह उस stopping point तक न पहुंचे।

उदाहरण: Recursive Factorial

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int factorial(int n) {
	// Check whether n == 0
	if (n == 0) {
		// Send 1 back to the caller
		return 1;
	}
	// Send n * factorial(n - 1) back to the caller
	return n * factorial(n - 1);
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", factorial(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

पहले N Numbers का Sum

1 से N तक numbers को recursively sum करना same तरह काम करता है: N का sum N plus (N-1) का sum है, हर call में problem को एक-एक करके तब तक shrink करते हुए जब तक यह 0 या 1 के base case तक न पहुंचे।

उदाहरण: Sum of First N Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int sum(int n) {
	// Check whether n <= 1
	if (n <= 1) {
		// Send n back to the caller
		return n;
	}
	// Send n + sum(n - 1) back to the caller
	return n + sum(n - 1);
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", sum(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Stack Overflow Warning

हर recursive call अपने local variables और return address track करने के लिए program के call stack पर अपनी जगह reserve करता है; recursion जो कभी अपने base case तक नहीं पहुंचता (या बहुत गहरा जाता है) उस stack को खत्म कर देता है और एक stack overflow के साथ crash हो जाता है।

उदाहरण: Stack Overflow Warning

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int factorial(int n) {
	// Check whether n == 0
	if (n == 0) {
		// Send 1 back to the caller
		return 1;
	}
	// Send n * factorial(n - 1) back to the caller
	return n * factorial(n - 1);
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", factorial(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. base case भूल जाना, ताकि function हमेशा के लिए खुद को call करता रहे और stack overflow हो जाए।
  2. base case की ओर move न करना, जैसे f(n - 1) की बजाय f(n) call करना।
  3. बड़े inputs के लिए naive recursion इस्तेमाल करना, जो धीमा है या stack overflow करता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.