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

Named Arguments क्या हैं

Named arguments आपको सिर्फ order पर निर्भर रहने के बजाय नाम से बिल्कुल बताने देते हैं कि आप कौन सा parameter भर रहे हैं।
Syntax
kotlin
fun functionName(param1: Type, param2: Type = defaultValue) { }

functionName(param1 = value1, param2 = value2)
functionName(param2 = value2, param1 = value1)

Basic Named Arguments

position पर निर्भर रहने के बजाय, आप किसी function को call करते समय explicitly parameterName = value specify कर सकते हैं, जो call को self-documenting बनाता है।

उदाहरण: Basic Named Arguments

markup
// Define the function `createRectangle` taking `width`, `height` and returning a String
fun createRectangle(width: Int, height: Int): String {
    // Return `"Rectangle ${width}x$height"` from this function
    return "Rectangle ${width}x$height"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `createRectangle(width = 10, height = 5)` to the console, with a trailing newline
    println(createRectangle(width = 10, height = 5))
}

Reordering with Named Arguments

चूंकि named arguments position के बजाय नाम से match होते हैं, उन्हें declare होने से अलग order में पास किया जा सकता है।

उदाहरण: Reordering with Named Arguments

markup
// Define the function `createRectangle` taking `width`, `height` and returning a String
fun createRectangle(width: Int, height: Int): String {
    // Return `"Rectangle ${width}x$height"` from this function
    return "Rectangle ${width}x$height"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `createRectangle(height = 5, width = 10)` to the console, with a trailing newline
    println(createRectangle(height = 5, width = 10))
}

Skipping Defaults with Named Arguments

Named arguments आपको सिर्फ एक बाद वाले default parameter को override करने देते हैं बिना उन पहले वाले defaults को दोहराए जिन्हें आप रखना चाहते हैं।

उदाहरण: Skipping Defaults with Named Arguments

markup
// Define the function `createUser` taking `name`, `age`, `active` and returning a String
fun createUser(name: String, age: Int = 18, active: Boolean = true): String {
    // Return `"$name (age $age, active=$active)"` from this function
    return "$name (age $age, active=$active)"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `createUser("Carol", active = false)` to the console, with a trailing newline
    println(createUser("Carol", active = false))
}

Improving Readability

एक जैसे type के कई parameters वाले functions के लिए, जैसे कई booleans, named arguments call site को एक नज़र में समझना कहीं आसान बना देते हैं।

उदाहरण: Improving Readability

markup
// Define the function `configure` taking `darkMode`, `notifications`, `autoSave`
fun configure(darkMode: Boolean, notifications: Boolean, autoSave: Boolean) {
    // Print "dark=$darkMode, notify=$notifications, autoSave=$autoSave" to the console, with a trailing newline
    println("dark=$darkMode, notify=$notifications, autoSave=$autoSave")
}

// Entry point: execution of the program starts here
fun main() {
    configure(darkMode = true, notifications = false, autoSave = true)
}
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. named और positional arguments को गलत तरीके से मिलाना; एक बार आप एक named argument उपयोग करें, उसके बाद के arguments को आमतौर पर named भी होना चाहिए।
  2. यह मान लेना कि named arguments performance या behavior बदलते हैं; वे सिर्फ readability और call-site flexibility को प्रभावित करते हैं।
  3. कई Boolean parameters वाले functions के लिए named arguments का उपयोग न करना, call sites को पढ़ना मुश्किल बनाते हुए (जैसे f(true, false, true))।
चैप्टर सारांश
  • Named arguments parameterName = value के रूप में पास किए जाते हैं, declaration order से स्वतंत्र।
  • वे कई parameters या एक जैसे type के कई parameters वाले functions के लिए readability को काफी सुधारते हैं।
  • Named arguments को default parameters के साथ combine करके बाद वाला सेट करते हुए पहले वाले defaults छोड़े जा सकते हैं।
  • एक बार named argument उपयोग होने पर, उस call में बाद के किसी भी arguments को स्पष्टता के लिए भी named होना चाहिए (और कभी-कभी होना ज़रूरी है)।
🔒

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.