Kotlin tutorial for Beginners – Kotlin Default and Named Arguments

Kotlin Default and Named Arguments

In this article, you will learn about default and named arguments with the help of examples.

Kotlin Default Argument

In Kotlin, you can provide default values to parameters in function definition.

If the function is called with arguments passed, those arguments are used as parameters. However, if the function is called without passing argument(s), default argument are used.


How default arguments works?

Case I: All arguments passed


Both arguments passed to the function

The function foo() takes two arguments. The arguments are provided with default values. However, foo() is called by passing both arguments in the above program. Hence, the default arguments are not used.

The value of letter and number will be 'x' and 2 respectively inside the foo() function.

Case II: All arguments are not passed


All arguments are not passed to the function

Here, only one (first) argument is passed to the foo() function. Hence, the first argument uses the value passed to the function. However, second argument number will take the default value since the second argument is not passed during function call.

The value of letter and number will be 'y' and 15 respectively inside the foo() function.

Case III: No argument is passed


No arguments passed to the function

Here, the foo() function is called without passing any argument. Hence, both arguments uses its default values.

The value of letter and number will be 'a' and 15 respectively inside the foo() function.


Example: Kotlin Default Argument


fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}

fun main(args: Array<String>) {
    println("Output when no argument is passed:")
    displayBorder()

    println("nn'*' is used as a first argument.")
    println("Output when first argument is passed:")
    displayBorder('*')

    println("nn'*' is used as a first argument.")
    println("5 is used as a second argument.")
    println("Output when both arguments are passed:")
    displayBorder('*', 5)

}

When you run the program, the output will be:

Output when no argument is passed:
===============

'*' is used as a first argument.
Output when first argument is passed:
***************

'*' is used as a first argument.
5 is used as a second argument.
Output when both arguments are passed:
*****

Kotlin named argument

Before talking about named argument, let us consider a little modification of the above code:


fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}

fun main(args: Array<String>) {
    displayBorder(5)
}

Here, we are trying to pass second argument to the displayBorder() function, and use default argument for first argument. However, this code will give use an error. It’s because the compiler thinks we are trying to provide 5 (Int value) to character (Char type).

To solve this situation, named arguments can be used. Here’ how:


Example: Kotlin named argument


fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}

fun main(args: Array<String>) {
    displayBorder(length = 5)
}

When you run the program, the output will be:

=====

In the above program, we are using named argument (length = 5) specifying that the length parameter in the function definition should take this value (doesn’t matter the position of the argument).

Named Arguments in Kotlin

The first argument character uses the default value '=' in the program.

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.