Kotlin functions are defined with “fun” keyword

You have already used functions like print and println in hello world lesson. Those are called predefined standard library. There is also user defined functions which is the functions you made yourself.

In this lesson we’re going to talk about more about functions. This is going to be fun!

Function is a named sub-program which performs a specific task. We need to call the function by name to execute the sub-program. You can think of a function as a machine.

Function as a machine

Functions takes inputs and process it into outputs. Inputs in a function are called parameters and output is the return value. We will talk more about parameters and return value later. All you need to know is that function is like mini machine in your program which responsible to do certain things like processing data.

Kotlin functions can be declared at top level in a file. Different from methods in Java which you have to create a class to hold them.

Functions are used because of following reasons :

  1. Functions improve the readability of code. By giving names to functions, it’s easier to read code because we know what each sub-program is for.
  2. Functions improve the reusability of the code. Functions can be called more than once, so you can and should use a function to solve specific tasks without having to keep repeating writing same code. Remember DRY, don’t repeat yourself.
  3. Functions make it easier to trace errors thus faster debugging process.

Let’s see how we define a function in Kotlin. Here is the base syntax to define a function :

fun functionName(parameter: DataType): ReturnType { 
 // function body here 
 }
fun functionName(parameter: DataType): ReturnType { 
 // function body here 
 }
Kotlin function base syntax

Here is the example :

Function which doubles an integer

In the above example, getGreeting() function takes a parameter which has type of String and returns a value also of type String.

To return a value means to output. Just like in the machine illustration before, the machine takes inputs and process them into output. In a funtion, the inputs are parameters and the output is the return value.

The values which you pass when you call a function for the parameters are called arguments. You have to provide arguments with the right data types, which means if you define a function which has a parameter of string, you must pass a string value when calling the function.

You can also define a method which returns no value at all. This function will automatically return Unit type. Unit type is basically equivalent to “nothing useful”. Funtion which returns no value at all don’t need return type in the definition.

printGreeting() is a function without return type

Just like naming a variable, naming a function has some convention which you should follow :

  1. Use camelCase. camelCase is the way to write phrases without spaces or punctuation, and for multiple word phrases we separate them by capitalizing the first character of each word.
  2. Use verbs which describe what your function does (e.g. calculateTotal, sendMessage, stop, getName, setColor).
  3. Function names should be as short as possible so that it’s simple to type and easy to remember.

When a function only has a single expression on its body, the curly braces can be omitted and the body is specified after a “=” symbol. You also can ommit the return type since the compiler can infer it from the expression.

Single-expression functions

All right! That’s it for Kotlin functions. You can save this article for your future reference. Thank you so much for reading. If you like these articles, let me know in the comment section below. You can also give suggestion, I’m open for any kind of feedback.

— Learn Kotlin Series —

  1. Learn Kotlin: Introduction
  2. Learn Kotlin: Hello World!
  3. Learn Kotlin: Variables
  4. Learn Kotlin: Data Types
  5. Learn Kotlin: Basic Operators
  6. Learn Kotlin: Functions (this article)

— Dictionary —

  1. Debugging : the process of finding and resolving bugs within program.
  2. DRY (Don’t Repeat Yourself) : a principle of software development aimed at reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.

— References —

  1. Kotlin functions — https://kotlinlang.org/docs/functions.html
  2. Kotlin style guide — https://developer.android.com/kotlin/style-guide

Android Developer. Currently residing in Jambi, Indonesia. https://romnan.my.id