← Back to Go Course | Chapter 4: Functions | Lesson 7 of 7

Recursion क्या है

Recursion तब है जब कोई function किसी बड़ी समस्या को खुद की थोड़ी छोटी copy से मदद मांगकर हल करता है, बार-बार, जब तक समस्या सीधे जवाब देने लायक छोटी न हो जाए।
Syntax
go
func name(n int) int {
    if n <= baseCase {
        return value
    }
    return name(n - 1)
}

A Basic Recursive Function

एक recursive function खुद को call करता है, आमतौर पर हर बार एक छोटे या सरल input के साथ, जब तक यह एक base case तक न पहुंचे जिसका बिना और recursion के सीधे जवाब दिया जा सके। Factorial classic पहला उदाहरण है।

उदाहरण: A Basic Recursive Function

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function factorial
func factorial(n int) int {
	// Check whether n <= 1
	if n <= 1 {
		// Send 1 back to the caller
		return 1
	}
	// Send n * factorial(n-1) back to the caller
	return n * factorial(n-1)
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(factorial(5))
}

The Importance of a Base Case

recursion को रोकने वाले base case के बिना, एक recursive function हमेशा के लिए खुद को call करता है (जब तक program stack खत्म होने से crash न हो जाए)। base case वह है जो calls की एक अंतहीन chain को एक असल में terminate होने वाले function में बदल देता है।

Note: हमेशा पुष्टि करें कि recursive argument हर call पर base case की ओर बढ़ रहा है।

उदाहरण: The Importance of a Base Case

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function countdown
func countdown(n int) {
	// Check whether n <= 0
	if n <= 0 {
		// Print the values followed by a newline
		fmt.Println("liftoff!")
		return
	}
	// Print the values followed by a newline
	fmt.Println(n)
	countdown(n - 1)
}

// main is where the program starts running
func main() {
	countdown(3)
}

Recursion on Recursive Data

Recursion उस data के लिए खासतौर पर स्वाभाविक है जो खुद recursively structured है, जैसे किसी slice का पहला element बाकी के sum में जोड़कर sum compute करना -- हर recursive call एक जैसी समस्या के shape का एक छोटा टुकड़ा handle करता है।

उदाहरण: Recursion on Recursive Data

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function sumSlice
func sumSlice(nums []int) int {
	// Check whether len(nums) == 0
	if len(nums) == 0 {
		// Send 0 back to the caller
		return 0
	}
	// Send nums[0] + sumSlice(nums[1:]) back to the caller
	return nums[0] + sumSlice(nums[1:])
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(sumSlice([]int{1, 2, 3, 4, 5}))
}
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 भूल जाना, जिससे stack overflow से program crash होने तक infinite recursion होता है।
  2. memoization के बिना recursive Fibonacci या समान लिखना, बड़े inputs के लिए exponential recomputation का कारण बनते हुए।
  3. यह मान लेना कि Go कुछ functional languages की तरह tail-recursive calls को अपने आप optimize करता है -- यह नहीं करता, इसलिए गहरी recursion अब भी stack space खर्च करती है।
चैप्टर सारांश
  • एक recursive function original समस्या के एक छोटे version के साथ खुद को call करता है।
  • हर recursive function को recursion रोकने के लिए एक base case चाहिए।
  • Go tail-call optimization नहीं करता, इसलिए बहुत गहरी recursion stack खत्म कर सकती है।
  • Recursion उन समस्याओं के लिए स्वाभाविक रूप से उपयुक्त है जिनकी स्वाभाविक रूप से recursive structure हो, जैसे tree traversal।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.