← Back to Kotlin Course | Chapter 1: Setup & Basics | Lesson 5 of 7

main() Function क्या है

main() function वह खास starting point है जो Kotlin को बताता है कि आपका program चलाना बिल्कुल कहां शुरू करना है।
Syntax
kotlin
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()

markup
// 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")
}

Accepting Command-Line Arguments

Kotlin fun main(args: Array<String>) भी support करता है, जहां args program launch होने पर command line पर पास किए गए किसी भी arguments को रखता है, जैसे java -jar app.jar hello world।

Note: अगर कोई arguments पास नहीं हुए, तो args बस एक खाली array है, null नहीं।

उदाहरण: Accepting Command-Line Arguments

markup
// 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")
    }
}

Calling Other Functions from main()

असली programs काम को छोटे functions में बांटते हैं और उन्हें main से call करते हैं। यह main को छोटा और readable रखता है, program क्या करता है उसकी एक high-level outline की तरह काम करते हुए।

उदाहरण: Calling Other Functions from main()

markup
// 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()")
}

Program Exit

एक Kotlin program स्वाभाविक रूप से तब खत्म होता है जब main अपना आखिरी statement चलाना खत्म कर देता है। main के अंदर return उपयोग करना इसे जल्दी exit करता है, उस line के बाद लिखे किसी भी code को skip करते हुए।

Note: return के बाद unreachable code से बचें -- compiler आपको इसके बारे में चेतावनी देगा।

उदाहरण: Program Exit

markup
fun main() {
    println("Before return")
    return
    // The next line would never run
}
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. अलग-अलग files में एक से ज़्यादा main() function define करना बिना यह समझे कि build असल में कौन सा चलाता है।
  2. यह मान लेना कि main को हमेशा एक String array parameter चाहिए; Kotlin एक parameterless fun main() भी अनुमति देता है।
  3. यह भूल जाना कि 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 के लिए सिर्फ एक main function होना चाहिए; अलग targets के रूप में उपयोग होने पर कई files की अपनी हो सकती हैं।
  • main दूसरे functions को call कर सकता है, और main के execute होना खत्म करते ही program खत्म हो जाता है।
🔒

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.