Default Parameters क्या हैं
In this page:
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
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
Overriding a Default
कोई caller फिर भी default वाले किसी parameter के लिए एक value पास कर सकता है, जो सिर्फ इस particular call के लिए वह default override कर देता है।
उदाहरण: Overriding a Default
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
Multiple Default Parameters
किसी function में defaults वाले कई parameters हो सकते हैं, callers को trailing वालों के किसी भी combination को स्वतंत्र रूप से छोड़ने देते हुए।
उदाहरण: Multiple Default Parameters
// 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))
}
Login to try C/C++/Java/PHP code in the editor
Defaults Avoid Overloading
arguments के हर combination के लिए कई overloaded functions लिखने के बजाय, defaults वाला एक single function सभी आम cases को संक्षेप में cover कर देता है।
उदाहरण: Defaults Avoid Overloading
// 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)}")
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि optional parameters के लिए Kotlin को Java जैसी overloaded functions चाहिए, बजाय defaults वाले एक single function उपयोग करने के।
- named arguments उपयोग किए बिना default value वाले किसी parameter के बाद एक non-default parameter रखना, जो अजीब call sites पर मजबूर कर सकता है।
- यह भूल जाना कि 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: