Lesson 19 – Functions with parameters with default value in kotlin

Lesson 19 – Functions with parameters with default value in kotlin

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

In Kotlin you can define parameters and assign them a data in the same header of the function. Then when we call the function we can or do not send a value to the parameter.

The default parameters allow us to create more flexible functions that can be used in different circumstances.

Problem 1

Create a function that receives a String as a parameter and optionally a second String with a character.The function must display the underlined String with the character indicated by the second parameter

Project92 – Home.kt

fun tituloSubrayado (title: String, character: String = "*") {
    println (title)
    for (i in 1..title.length)
        print (character)
    println ()
}

fun main (parameter: Array <String>) {
    titleUndered ("Management System")
    titleUndered ("Sales", "-")
}

When we run this application we can observe the following result:

Lesson 19 - Functions with parameters with default value in kotlin - Lesson 19 - Functions with parameters with default value in kotlin - Lesson 19 - Functions with parameters with default value in kotlin -  Functions with parameters with default value in kotlin
Functions with parameters with default value in kotlin

The first important thing to note is that the call to the function titled Underline can be done by sending a data or two data:

    TitleUndered ("Management System")
    TitleUndered ("Sales", "-")

This could not be correct if we do not use a special syntax when declaring the parameters of the function:

fun tituloSubrayado (title: String, character: String = "*") {
    println (title)
    for (i in 1..title.length)
        print (character)
    println ()
}

As we see the character parameter has an assignment of a default value for the cases that we call the function with a single parameter.

When we call the function titleSubressed with a single parameter then the character parameter stores the value “*”. If we call the function and pass two parameters in our example the character parameter stores the string “-”

The algorithm of the function print the first parameter and by a for that is repeated as many times as the length of the “title” we print the second parameter

Proposed problem

  • Make a function that receives between 2 and 5 integers. It must return the sum of these values. Must have three default parameters.

Solution

Project93
fun sum (v1: Int, v2: Int, v3: Int = 0, v4: Int = 0, v5: Int = 0) = v1 + v2 + v3 + v4 + v5

fun main (parameter: Array <String>) {
    println ("The sum of 5 + 6 is $ {sum (5,6)}")
    println ("The sum of 1 + 2 + 3 is $ {sum (1,2,3)}")
    print ("The sum of 1 + 2 + 3 + 4 + 5 is $ {sum (1,2,3,4,5)}")
}

About The Author

Related posts

Leave a Reply