Beginner 4 min readKotlin 2.0

The Kotlin main Function — Entry Point Explained

Every runnable Kotlin program starts from the main function. This tutorial explains its signature and how to use command-line arguments.

What You Will Learn

  • Why main() is the entry point
  • The modern no-args signature
  • How to read command-line args
  • What happens when main returns

The main Function

When you run a Kotlin program, the runtime looks for a top-level function called main. This is where execution begins. Since Kotlin 1.3, the simplest form has no parameters at all.

Simple main Function

kotlin
fun main() {
    println("Program started")
    println("Program ended")
}
Output
Program started Program ended

fun main() with no parameters is the modern Kotlin entry point. The code runs top to bottom. When main returns, the program ends.

Beginner Tip: You can only have one main function per runnable program. If you have multiple files, exactly one should contain the main function.

Command-Line Arguments

If you need to read arguments passed at startup, use the form fun main(args: Array<String>). Each word after the program name becomes an element in args.

Reading Command-Line Args

kotlin
fun main(args: Array<String>) {
    if (args.isEmpty()) {
        println("No arguments provided")
    } else {
        println("First argument: ${args[0]}")
    }
}
Output
No arguments provided

args is an Array of Strings. args.isEmpty() checks if the array has no elements. args[0] gets the first argument (zero-based index).

In the Kotlin Playground, you cannot pass command-line arguments. This feature is used in real applications run from the terminal.

Practice Exercise

Exercisepredict output

What does this print? fun main() { println("First") println("Second") }

Quick Quiz

Quick Quiz

Since which Kotlin version can main() have no parameters?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-01Kotlin 2.0

Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review