main() Function क्या है
In this page:
fun main() {
// no parameters
}
fun main(args: Array<String>) {
// args holds command-line arguments
}
Declaring main()
सबसे सरल entry point fun main() { ... } है, जो कोई parameters नहीं लेता। यह ज़्यादातर command-line programs के लिए काफी है, जिसमें इस course में कवर की हर चीज़ शामिल है।
उदाहरण: Declaring main()
// Entry point: execution of the program starts here
fun main() {
// Print "main() with no parameters" to the console, with a trailing newline
println("main() with no parameters")
}
Login to try C/C++/Java/PHP code in the editor
Accepting Command-Line Arguments
Kotlin fun main(args: Array<String>) भी support करता है, जहां args program launch होने पर command line पर पास किए गए किसी भी arguments को रखता है, जैसे java -jar app.jar hello world।
args बस एक खाली array है, null नहीं।उदाहरण: Accepting Command-Line Arguments
// Define the function `main` taking `args`
fun main(args: Array<String>) {
// Print "Received ${args.size} arguments" to the console, with a trailing newline
println("Received ${args.size} arguments")
// Loop over `args`, binding each item to `arg`
for (arg in args) {
// Print "Argument: $arg" to the console, with a trailing newline
println("Argument: $arg")
}
}
Login to try C/C++/Java/PHP code in the editor
Calling Other Functions from main()
असली programs काम को छोटे functions में बांटते हैं और उन्हें main से call करते हैं। यह main को छोटा और readable रखता है, program क्या करता है उसकी एक high-level outline की तरह काम करते हुए।
उदाहरण: Calling Other Functions from main()
// Define the function `greet` taking `name`
fun greet(name: String) {
// Print "Hello, $name!" to the console, with a trailing newline
println("Hello, $name!")
}
// Entry point: execution of the program starts here
fun main() {
greet("Kotlin")
// Print "Back in main() after calling greet()" to the console, with a trailing newline
println("Back in main() after calling greet()")
}
Login to try C/C++/Java/PHP code in the editor
Program Exit
एक Kotlin program स्वाभाविक रूप से तब खत्म होता है जब main अपना आखिरी statement चलाना खत्म कर देता है। main के अंदर return उपयोग करना इसे जल्दी exit करता है, उस line के बाद लिखे किसी भी code को skip करते हुए।
return के बाद unreachable code से बचें -- compiler आपको इसके बारे में चेतावनी देगा।उदाहरण: Program Exit
fun main() {
println("Before return")
return
// The next line would never run
}
Login to try C/C++/Java/PHP code in the editor
- अलग-अलग files में एक से ज़्यादा
main()function define करना बिना यह समझे कि build असल में कौन सा चलाता है। - यह मान लेना कि
mainको हमेशा एकStringarray parameter चाहिए; Kotlin एक parameterlessfun main()भी अनुमति देता है। - यह भूल जाना कि
mainके अंदरreturnके बाद रखा code कभी नहीं चलेगा।
fun main()किसी standalone Kotlin program का entry point है; execution हमेशा वहीं से शुरू होता है।- Kotlin optionally command-line arguments पाने के लिए
fun main()औरfun main(args: Array<String>)दोनों को support करता है। - हर compiled entry point के लिए सिर्फ एक
mainfunction होना चाहिए; अलग targets के रूप में उपयोग होने पर कई files की अपनी हो सकती हैं। mainदूसरे functions को call कर सकता है, औरmainके execute होना खत्म करते ही program खत्म हो जाता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: