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.

Kotlin Program with main() Function

Following is a pictorial representation of the Kotlin file with main() function. There is also a data class, and some helper functions, but they are not in focus of this tutorial. We shall discuss about main() function in detail.

main function in Kotlin - Kotlin Tutorial - www.tutorialkart.com

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 should contain the main() function.

Example – Kotlin main()

Following is an example Kotlin program, with main() function.

This Kotlin program has a mina() function, some usual function, and a data class.

When you run the above program, the program control start with the main function. It first loads the arguments passed while running the program to the args Array. And then it executes the program statements of main() function one by one.

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

Conclusion

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