Hello, budding developer! Today, we're exploring function parameters in Kotlin. Function parameters are the inputs given to functions. For instance, in println("Hello, world!"), "Hello, world!" is a parameter you are passing to the println() function. Parameters help functions adapt to specific pieces of information, making them incredibly versatile.
We'll start by writing a function that takes a single parameter. This simple function will greet a user:
In this case, name is a parameter of the type String that our greetUser function uses.
We can also create a function with multiple parameters. For example, a function could calculate a rectangle's area by accepting its length and width as parameters.
In this code, length and breadth are Int parameters.
Kotlin supports different data types for parameters. For example, Int, Double, Boolean, String, Array etc. We've worked with String and Int type function parameters in the examples above.
A Kotlin function can take any number of parameters -- even zero. Here's an example:
