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

Default Parameters क्या हैं

Default parameters किसी function को पहले से यह जानने देते हैं कि अगर आप खुद एक देने की परवाह नहीं करते तो कौन सी value उपयोग करनी है।
Syntax
kotlin
fun functionName(param: Type, optional: Type = defaultValue): ReturnType {
    // body
}

functionName(value)
functionName(value, otherValue)

Declaring a Default Value

parameter list में सीधे एक value assign करना, जैसे greeting: String = "Hello", उस parameter को optional बना देता है -- जो callers इसे पास नहीं करते उन्हें default मिल जाता है।

उदाहरण: Declaring a Default Value

markup
// Define the function `greet` taking `name`, `greeting` and returning a String
fun greet(name: String, greeting: String = "Hello"): String {
    // Return `"$greeting, $name!"` from this function
    return "$greeting, $name!"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `greet("Kotlin")` to the console, with a trailing newline
    println(greet("Kotlin"))
}

Overriding a Default

कोई caller फिर भी default वाले किसी parameter के लिए एक value पास कर सकता है, जो सिर्फ इस particular call के लिए वह default override कर देता है।

उदाहरण: Overriding a Default

markup
// Define the function `greet` taking `name`, `greeting` and returning a String
fun greet(name: String, greeting: String = "Hello"): String {
    // Return `"$greeting, $name!"` from this function
    return "$greeting, $name!"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `greet("Kotlin", "Welcome")` to the console, with a trailing newline
    println(greet("Kotlin", "Welcome"))
}

Multiple Default Parameters

किसी function में defaults वाले कई parameters हो सकते हैं, callers को trailing वालों के किसी भी combination को स्वतंत्र रूप से छोड़ने देते हुए।

उदाहरण: Multiple Default Parameters

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("Alice")` to the console, with a trailing newline
    println(createUser("Alice"))
    // Print `createUser("Bob", 25)` to the console, with a trailing newline
    println(createUser("Bob", 25))
}

Defaults Avoid Overloading

arguments के हर combination के लिए कई overloaded functions लिखने के बजाय, defaults वाला एक single function सभी आम cases को संक्षेप में cover कर देता है।

उदाहरण: Defaults Avoid Overloading

markup
// Define the function `power` taking `base`, `exponent` and returning an Int
fun power(base: Int, exponent: Int = 2): Int {
    // Declare a mutable variable `result`, initialized to `1`
    var result = 1
    repeat(exponent) { result *= base }
    // Return `result` from this function
    return result
}

// Entry point: execution of the program starts here
fun main() {
    // Print "Square: ${power(5)}" to the console, with a trailing newline
    println("Square: ${power(5)}")
    // Print "Cube: ${power(5, 3)}" to the console, with a trailing newline
    println("Cube: ${power(5, 3)}")
}
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. यह मान लेना कि optional parameters के लिए Kotlin को Java जैसी overloaded functions चाहिए, बजाय defaults वाले एक single function उपयोग करने के।
  2. named arguments उपयोग किए बिना default value वाले किसी parameter के बाद एक non-default parameter रखना, जो अजीब call sites पर मजबूर कर सकता है।
  3. यह भूल जाना कि default values हर call के लिए फिर से evaluate होती हैं, एक बार compute होकर calls में share नहीं होतीं।
चैप्टर सारांश
  • एक default value function declaration में parameter: Type = value के रूप में लिखी जाती है।
  • Callers किसी भी parameter को छोड़ सकते हैं जिसका एक default हो, और इसके बजाय default value उपयोग होती है।
  • Default parameters कई cases में कई overloaded functions की ज़रूरत हटा देते हैं।
  • default parameters को named arguments के साथ combine करना कुछ defaults छोड़ते हुए बाकी को override करना आसान बना देता है।
🔒

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.