Default and Named Arguments in Kotlin

Learn via video courses
Topics Covered

Overview

Functions are like the foundation of computer programs, and how good the code turns out often relies on how these functions are made and used. Kotlin, a modern programming language, has two special features called default and named arguments. Default arguments in Kotlin provide the default values for the function parameters. These arguments are used when no value is provided. On the other hand, named arguments in Kotlin allow you to specify function arguments by name, rather than relying on their positional order.

Introduction

In Kotlin, functions and constructors can be equipped with default arguments and named arguments. These features make function calling and initializing objects easier by allowing you to specify default values for some or all of the function parameters. Default arguments in Kotlin enable you to call a function without specifying values for every parameter, while named arguments in Kotlin grant you the flexibility to provide values in any order by explicitly mentioning the parameter names.

Default Arguments in Kotlin

Default arguments in Kotlin allow you to provide default values for function parameters. This means that when you call a function, you can choose to provide values for some parameters while leaving others with their default values. Default arguments make functions more flexible because you can choose which parameters to customize and which to leave with their predefined values, making your code more concise and readable.

Example - 1: Greeting Function

Code:

Output:

Explanation:

In the above code, we have a function named greet with a name parameter set to "Guest". When we call greet() without an argument, it will use the default value, resulting in the output "Hello, Guest". But when we call greet() with an argument to the function, it will override the default value and say "Hello, Piyush!" This demonstrates how default arguments let you set a default value for the function parameter.

Example - 2: Calculator Function with Default Argument

Code:

Output:

Explanation:

In the above code, we have created a calculate function that performs basic arithmetic operations on two numbers. The operator parameter has a default value of "+", indicating addition. It allows you to perform addition by providing only the a and b parameters. You can also specify other operations if you want to perform different operations. Default arguments simplify function calls and enhance code readability, as you can see in the output, where the sum and product are calculated with ease.

Named Arguments in Kotlin

Named arguments in Kotlin allow you to pass values to function parameters by specifying the parameter names explicitly in a function call. With named arguments, you don’t have to remember and keep track of the sequence of parameters in the function signature. This makes code easier to read and reduces the chance of passing value to wrong parameters, particularly in functions with lots of arguments.

Example - 1: Restaurant Order Function with Named Arguments

Code:

Output:

Explanation:

In the above code, we have a function called placeOrder that takes some parameters to place an order at a restaurant. We have used named arguments in our function calls to make it easier to understand which value matches each parameter. This makes the code easier to read, and the output shows the details of the order with and without any special requests.

Example - 2: User Registration Function with Named Arguments

Code:

Output:

Explanation:

In the above code, we have created the registerUser function for registering users. The registerUser function takes parameters for the user details, including the optional subscriptionStatus. We can specify the values for these parameters by using named arguments in function calls. This results in more readable code, and you can observe the output showing user names and their subscription statuses.

Conclusion

  • Default and named arguments are two important features of Kotlin that enhance code readability and flexibility when working with functions and constructors.
  • Default arguments in Kotlin allow you to provide default values for parameters, making them optional during function calls.
  • Named arguments in Kotlin allow you to specify parameter values by explicitly stating the parameter names, allowing you to provide values in any order.
  • These features are particularly useful when dealing with functions or constructors that have multiple parameters, as they simplify function calls and make your code more maintainable.