Args in main parameters

What is the use of the args: Array in the main function. Because it seems useless to me

It’s optional

https://kotlinlang.org/docs/basic-syntax.html#program-entry-point

args contains the parameters passed to the executable.

1 Like

If you start your program from the command line, then those command-line arguments can be the primary means of telling it what to do.⠀(Especially for small utilities and suchlike.⠀For example, if you wrote a word-count program, you could tell it what text file(s) to scan by listing their names on the command line.)

Or they can provide extra information, configuration, or options.⠀(For example, an option could tell your counting program not to show each file’s count separately but only a grand total.)

And even if your program gets started some other way, e.g. from a GUI, it will often have arguments passed to it behind-the-scenes. (For example, you might be able to launch your program by dragging one or more files onto its icon; the names of those files would then be passed to it as command-line arguments.)

Kotlin allows you to omit the parameter if you know you’ll never be using it.⠀But many programs do.

1 Like