Kotlin main()

The main() function in Kotlin is the entry point to a Kotlin program.

Kotlin main() function can be related to main() function in Java programming or C programming language.

Kotlin supports both procedural programming and object oriented programming. If you have worked with some of the procedural languages, you may know that main() is the entry point to a program.

You can write other entities of a program, like a class, or some other functions next to the main function in your Kotlin file.

If you write a Kotlin file with no main() function, that could be only used as a package or library. But to run a Kotlin File, that file must contain the main() function.

Example

The following is a simple example for a Kotlin program with main() function. We have one more function sayHello() along with our main() function.

Kotlin Program

fun main() {
    sayHello("Arjun");
}

fun sayHello(name: String){
    println("Hello $name!")
}

When you run the program, you should get the following output in your console.

Output

Hello Arjun!
ADVERTISEMENT

Conclusion

In this Kotlin Tutorial, we have learned about main() function and its usage in a Kotlin application.