EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Kotlin Functions

In most programming languages, when calling a function, we are required to specify all the arguments that the function accepts. However, in Kotlin, this constraint is lifted, making function calls more flexible. Kotlin allows us to omit certain arguments when calling a function by providing default values for parameters. This is one of the powerful features of Kotlin, which simplifies function calls and enhances readability.

In Kotlin, function parameters are defined using Pascal notation (i.e., name: data_type) and separated by commas. By assigning default values to parameters, we can make them optional, allowing the function to use the provided defaults if arguments are not passed during the function call.

There are two types of arguments in Kotlin –  

1. Default arguments
2. Named arguments

Kotlin Default arguments 

The arguments which need not specify explicitly while calling a function are called default arguments. 
If the function is called without passing arguments then the default arguments are used as function parameters. In other cases, if arguments are passed during a function call then passed arguments are used as function parameters.

There are three cases for default arguments-  

1. No arguments are passed while calling a function
2. Partial arguments are passed while calling a function
3. All arguments are passed while calling a function

1. No arguments are passed while calling a function 

When no argument is passed while calling a function then the default arguments are used as function parameters. We need to initialize the variables while defining a function. 

Kotlin program of calling student() function without passing an arguments

// Function with default parameter values
fun functionName(param1: Type = defaultValue1, param2: Type = defaultValue2) {
    // Function body
}

// Function call examples
functionName()                        // Uses all default values
functionName(param1 = value1)          // Overrides param1, uses default for param2
functionName(param2 = value2)          // Overrides param2, uses default for param1
functionName(param1 = value1, param2 = value2)  // Overrides both param1 and param2
functionName(param2 = value2, param1 = value1)  // Named arguments allow reordering

Example:

// Function with default arguments for employee details
fun employeeDetails(name: String = "John", department: String = "HR", id: Int = 101) {
    println("Employee Name: $name")
    println("Department: $department")
    println("Employee ID: $id")
}

fun main() {
    // Calling the function with no arguments (uses all default values)
    employeeDetails()

    // Calling the function with one argument (overrides the default name)
    employeeDetails(name = "Alice")

    // Calling the function with two arguments (overrides name and department)
    employeeDetails(name = "Bob", department = "IT")

    // Calling the function with all arguments (overrides all default values)
    employeeDetails(name = "Charlie", department = "Finance", id = 105)
}

Output:

Hello, Guest!
Hello, Alice!
Hi, Bob!
Welcome, Charlie!

2. Partial arguments are passed while calling a function –

Here some of the arguments are passed while calling a function and these are used as function parameters. If any formal parameter does not get value from the function call then the default value will be used for that parameter.

Kotlin program of calling student() function with passing some arguments.

// Default arguments in function definition: name, department, and salary
fun employeeDetails(name: String = "John Doe", department: String = "HR", salary: Int = 50000) {
    println("Employee Name: $name")
    println("Department: $department")
    println("Salary: $salary")
}

fun main(args: Array<String>) {
    val employee_name = "Alice"
    val employee_department = "Finance"

    // Passing only two arguments: name and department
    employeeDetails(employee_name, employee_department)
}

Output:

Employee Name: Alice
Department: Finance
Salary: 50000

3. All arguments are passed while calling a function –

Here, we have to pass all the arguments as defined in the function definition but data type of actual arguments must match with data type of formal arguments in the same order. 

Kotlin program of calling student() function with passing all the arguments

// Default arguments in function definition: name, category, and price
fun productDetails(name: String = "Generic Product", category: String = "General", price: Double = 100.0) {
    println("Product Name: $name")
    println("Category: $category")
    println("Price: $$price")
}

fun main(args: Array<String>) {
    val product_name = "Laptop"
    val product_category = "Electronics"
    val product_price = 1500.0

    // Passing all the arguments of product name, category, and price in the same order as defined
    productDetails(product_name, product_category, product_price)
}

Output:

Product Name: Laptop
Category: Electronics
Price: $1500.0
End of lesson.