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

Nested Functions क्या हैं

एक nested function एक छोटा helper function है जो किसी दूसरे function के बिल्कुल अंदर define किया गया है, सिर्फ वहीं usable।
Syntax
swift
func outerFunction() {
    func innerFunction() {
        // can use outerFunction's variables
    }
    innerFunction()
}

Defining a Nested Function

एक function जो किसी दूसरे function की body के अंदर declared है सिर्फ उस enclosing function तक scoped है और multi-step logic organize करने में मदद करता है।

उदाहरण: Defining a Nested Function

markup
// Define the function `processOrder` taking `itemCount`, `pricePerItem` and returning a Double
func processOrder(itemCount: Int, pricePerItem: Double) -> Double {
    // Define the function `applyDiscount` taking `total` and returning a Double
    func applyDiscount(_ total: Double) -> Double {
        // Return `itemCount > 5 ? total * 0.9 : total` from this function
        return itemCount > 5 ? total * 0.9 : total
    }
    // Declare a constant `subtotal`, set to `Double(itemCount) * pricePerItem`
    let subtotal = Double(itemCount) * pricePerItem
    // Return `applyDiscount(subtotal)` from this function
    return applyDiscount(subtotal)
}
// Print `processOrder(itemCount: 8, pricePerItem: 10.0)` to the console
print(processOrder(itemCount: 8, pricePerItem: 10.0))

Nested Functions Capturing Outer Variables

एक nested function को अपने आप इसके enclosing function के constants और variables तक access होता है, उन्हें explicitly pass किए बिना।

Note: यह pattern -- state capture करने वाला एक nested function return करना -- closures की नींव है, जो एक बाद के chapter में covered है।

उदाहरण: Nested Functions Capturing Outer Variables

markup
// Define the function `makeCounter` with no parameters and returning a `() -> Int`
func makeCounter() -> () -> Int {
    // Declare a mutable variable `count`, initialized to `0`
    var count = 0
    // Define the function `increment` with no parameters and returning an Int
    func increment() -> Int {
        count += 1
        // Return `count` from this function
        return count
    }
    // Return `increment` from this function
    return increment
}
// Declare a constant `counter`, set to `makeCounter()`
let counter = makeCounter()
// Print `counter()` to the console
print(counter())
// Print `counter()` to the console
print(counter())
// Print `counter()` to the console
print(counter())
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. एक nested function को इसके enclosing function के बाहर से call करने की कोशिश करना; इसका scope इसके declared होने की जगह तक सीमित है।
  2. ऐसे logic के लिए nested functions overuse करना जो एक top-level function के रूप में ज़्यादा clear और reusable होता।
  3. यह भूल जाना कि एक nested function अपने enclosing function के scope से variables capture और उपयोग कर सकता है, बिल्कुल एक closure की तरह।
चैप्टर सारांश
  • किसी दूसरे function के अंदर defined एक function सिर्फ उस enclosing function के अंदर visible है।
  • Nested functions अपने enclosing scope से variables capture कर सकते हैं।
  • वे एक complex function को छोटे, well-named private steps में तोड़ने में मदद करते हैं।
  • एक nested function को इसके enclosing function से return किया जा सकता है अगर enclosing function का return type एक matching function type हो।
🔒

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.