Function के Basics
In this page:
fun functionName(param: Type): ReturnType {
return value
}
fun functionName(param: Type) { // no return value
}
Declaring a Function
एक function fun से शुरू होता है, उसके बाद उसका नाम, parentheses में एक parameter list, एक optional return type, और curly braces में एक body।
उदाहरण: Declaring a Function
// Define the function `greet` taking `name` and returning a String
fun greet(name: String): String {
// Return `"Hello, $name!"` from this function
return "Hello, $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
Functions with No Return Value
कोई function जो सिर्फ printing जैसा action करता है और कोई उपयोगी value produce नहीं करता उसका return type Unit होता है, जिसे छोड़ा जा सकता है क्योंकि यह default है।
उदाहरण: Functions with No Return Value
// Define the function `printBanner` taking `text`
fun printBanner(text: String) {
// Print "=== $text ===" to the console, with a trailing newline
println("=== $text ===")
}
// Entry point: execution of the program starts here
fun main() {
printBanner("Welcome")
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
कोई function commas से अलग किए कई typed parameters ले सकता है, और इनमें से हर एक (अगर उसका कोई default न हो) को call site पर देना ज़रूरी है।
उदाहरण: Multiple Parameters
// Define the function `add` taking `a`, `b` and returning an Int
fun add(a: Int, b: Int): Int {
// Return `a + b` from this function
return a + b
}
// Entry point: execution of the program starts here
fun main() {
// Print "Sum: ${add(3, 4)}" to the console, with a trailing newline
println("Sum: ${add(3, 4)}")
}
Login to try C/C++/Java/PHP code in the editor
Calling Functions
किसी function को call करना उसके नाम के बाद parentheses का उपयोग करता है जिनमें parameters declared होने के उसी order में arguments होते हैं।
उदाहरण: Calling Functions
// Define the function `multiply` taking `a`, `b` and returning an Int
fun multiply(a: Int, b: Int): Int {
// Return `a * b` from this function
return a * b
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `result`, set to `multiply(6, 7)`
val result = multiply(6, 7)
// Print "6 x 7 = $result" to the console, with a trailing newline
println("6 x 7 = $result")
}
Login to try C/C++/Java/PHP code in the editor
- जब कोई function implicitly
Unitreturn नहीं करता तब return type declare करना भूल जाना, एक compiler error का कारण बनते हुए। - positionally किसी function को call करते समय parameter order को मिला देना, गलत parameters को values भेजते हुए।
- यह न समझना कि बिना explicit
returnऔर एक block body वाला function implicitlyUnitreturn करता है, आखिरी expression की value नहीं।
- Functions को
fun name(parameters): ReturnType { body }से declare किया जाता है। - जिस function की कोई मायने रखने वाली return value नहीं है उसका return type
Unitहै (या छोड़ा जा सकता है)। - Parameters typed हैं और, defaults दिए गए न हों तो, function को call करते समय सभी देने चाहिए।
returnkeyword किसी block-bodied function से एक value वापस भेजता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: