Functions

Android development with Kotlin
3 min readMay 4, 2023

--

Kotlin is a modern, statically typed programming language that is designed to be concise, expressive, and safe. One of the key features of Kotlin is its support for functions. In this blog post, we will explore the basics of functions in Kotlin and how they can be used to write clean and reusable code.

What is a Function?

Functions are a fundamental building block of any programming language. In Kotlin, functions are first-class citizens, which means that they can be treated like any other value. Functions in Kotlin are similar to functions in other languages like Java, but they have a number of features that make them more powerful and flexible. In this blog, we will explore the different aspects of functions in Kotlin.

Declaring a Function

To declare a function in Kotlin, you use the fun keyword followed by the function name, a set of parentheses, and an optional return type. The function body is contained within curly braces, and the return keyword is used to specify the value that the function should return. Here is a simple example:

fun addNumbers(a: Int, b: Int): Int {
return a + b
}

In this example, we have declared a function named addNumbers that takes two integer parameters and returns an integer value. The body of the function contains a single statement that adds the two parameters together and returns the result.

Kotlin also allows you to define functions with default parameter values, which can be useful when you want to provide a default value for a parameter but still allow it to be overridden if needed. Here’s an example:

fun greet(name: String = "World") {
println("Hello, $name!")
}

In this example, we’re defining a function called greet that takes a single parameter (name), with a default value of “World”. If no value is provided for the “name” parameter when the function is called, it will default to “World”. We will discuss this approach in upcoming blogs.

Calling a Function in Kotlin

Once a function is defined, it can be called from other parts of the program. To call a function in Kotlin, you simply specify the function name followed by the input parameters (if any) enclosed in parentheses. Here’s an example:

val result = add(2, 3)
println(result) // Output: 5

In this example, we call the add function with two input parameters 2 and 3. The function returns the sum of the two numbers, which is then stored in the result variable. Finally, the result is printed on the console.

Working with Parameters

In Kotlin, you can specify default values for function parameters. This means that if a function is called without providing a value for a particular parameter, the default value will be used instead. Here’s an example:

fun greet(name: String, greeting: String = "Hello") {
println("$greeting, $name!")
}

In this example, we have defined a function called greet that takes two parameters - name and greeting. The greeting parameter has a default value of "Hello". This means that if the greeting parameter is not provided when the function is called, it will default to "Hello". Here's how we can call this function:

greet("John") // Output: Hello, John!
greet("Mary", "Hi") // Output: Hi, Mary!

Return Values

Functions can also return values, which are specified using the return keyword. Here's an example:

fun isEven(number: Int): Boolean {
return number % 2 == 0
}

This function, named isEven, accepts a single integer parameter named number and returns true if the number is even, and false if it's odd. We can call this function like so:

val result = isEven(4)

The value of result will be true since 4 is an even number.

Unit Returning Functions:

If a function does not return a useful value, its return type is Unit. Unit is a type with only one value which is Unit.

fun sum(a: Int, b: Int): Unit{
val x = a+b
println(x)
}

The Unittype declaration is also optional. The above code is equivalent to:

fun sum(a: Int, b: Int): Unit{
val x = a+b
println(x)
}

Conclusion

Functions are a core concept in Kotlin, and understanding how to declare, call, and use functions is essential for any Kotlin developer. In this blog, we have explored the different aspects of functions in Kotlin, including declaring functions, calling functions, and function parameters. By mastering these concepts, you can write more expressive and powerful Kotlin code.

Learn Lambda expression

Learn Higher Order function

Please let me know your suggestions and comments. Follow for more such easy and helpful tutorials.

Thanks for reading…

Happy Coding!

--

--

Responses (1)