Call to function with named arguments in Kotlin language. This feature of Kotlin allows us to call the function indicating in any order the parameters of the same, but we must specify in the call the name of the parameter and the value to send.

Topic: Call to function with named arguments in Kotlin language.

Problem 1

Make a function that receives the name of an operator, the hourly payment and the number of hours worked. You must show your salary and name. Call the function using named arguments.

Solution

Project94 – Principal.kt

fun calculateString (name: String, costTime: Double, amountHours: Int) {
    val salary = costTime * amountHours
    println ("$name work $amountHours hours, you are paid per hour $costTime therefore you are entitled to a salary of $salary")

}

fun main (parameter: Array <String>) {
    calculateString ("juan", 10.5, 120)
    calculateString (costTime = 12.0, amountHours = 40, name = "Mother")
    calculateString (amountHours = 90, name = "luis", costTime = 7.25)
}
lesson 20 - Call to function with named arguments in Kotlin language - lesson 20 - Call to function with named arguments in Kotlin language - lesson 20 - Call to function with named arguments in Kotlin language - Call to function with named arguments in Kotlin language

Call to function with named arguments in Kotlin language

As we can see there is no change when we define the function:

fun calculateString (name: String, costTime: Double, amountHours: Int) {
    val salary = costHora * amountHours
    println ("$worked $numberHours hours, you are paid per hour $costHora therefore you are entitled to a salary of $salary")
}

We can call it as we already know by indicating the values directly:

    CalculateSueldo ("juan", 10.5, 120)

But we can also indicate the data in any order but with the obligation to precede the name of the parameter:

    CalculateSueldo (costeHora = 12.0, amountHours = 40, name = "ana")
    CalculateSueldo (amountHours = 90, name = "luis", costHora = 7.25)

Proposed problem

  • Develop a function that shows the multiplication table of the value that we send as a parameter. Define a second parameter called term that defaults to store the value 10. You must show as many terms of the multiplication table as indicated by the second parameter.
    Call the function from the main with named arguments.

Topic: Call to function with named arguments in Kotlin language.

Project95

fun table (number: Int, terms: Int = 10) {
    for (i in number..number * terms step number)
        println (i)
}
// coding180.com
fun main (parameter: Array <String>) {
    println ("Table of 3")
    table (3)
    println ("Table of 3 with 5 terms")
    table (3, 5)
    println ("Table of 3 with 20 terms")
    table (terms = 20, number = 3)
}

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!