Functions in Kotlin

Functions are one of the most important parts of programming. We write tons of functions every week for our projects. Functions are also a part of the fundamentals of programming. To learn functional programming, we must have our concepts clear with regard to functions. In this section, we will cover the basics of functions, in order to get you brushed up and ready for the next sections in this chapter, where we will be discussing abstract functional concepts and their implementation in Kotlin.

So, let's start by defining functions.

A function is a block of organized, reusable code that is used to perform a single, related action.

Not very clear? We will explain, but first, let's learn why we should write functions. In short, what is the functionality of a function? Have a look:

  • Functions allow us to break the program into a bunch of steps and substeps
  • Functions encourages code reuse
  • Functions, if used properly, help us keep the code clean, organized, and easy to understand
  • Functions make testing (unit testing) easy, testing each small part of the program is easier than the complete program in a single go

In Kotlin, a function generally looks like the following:

fun appropriateFunctionName(parameter1:DataType1, parameter2:DataType2,...): ReturnType { 
    //do your stuff here 
    return returnTypeObject 
} 

In Kotlin, a function declaration starts with the fun keyword, followed by the function name, then braces. Inside the braces, we can specify function arguments (optional). After the braces, there would be a colon (:) and return type, which specifies the datatype of the value/object to be returned (you can skip return type if you don't plan to return anything from the function; in that case, the default return type Unit will be assigned to the function). After those, there would be the function body, covered in curly braces (curly braces are also optional for Single-Expression functions, covered next in Chapter 5, More on Functions).

Unit is a datatype in Kotlin. Unit is a singleton instance of itself and holds a value that is Unit itself. Unit corresponds to void in Java, but it's quite different than void. While void means nothing in Java and void cannot contain anything, we have Nothing in Kotlin for that purpose, which indicates that a function would never complete successfully (due to an exception or an infinite loop).

Now, what are those return types, parameters (arguments), and function bodies? Let's explore them.

The following is a more realistic function example than the abstract one previously shown:

fun add(a:int, b:Int):Int { 
   val result = a+b 
   return result 
} 

Now, have a look at the following explanations for each parts of a function:

  • Function arguments/parameters: These are the data (unless lambda) for the function to work on. In our example, a and b are the function parameters.
  • Function body: Everything we write inside the curly braces of a function is called the function body. It is the part of a function, where we write the logic or set of instructions to accomplish a particular task. In the preceding example, two lines inside the curly braces is the function body.
  • Return statement, datatype: If we are willing to return some value from the function, we have to declare the datatype of the value we are willing to return; that datatype is called the return type—in this case, Int is the return type and return result is the return statement, it enables you to return a value to the calling function.

We can make the previous example shorter by removing val result = a+b and replacing the return statement with return a+b. In Kotlin, we can further shorten this example, as we will see in Chapter 5More on Functions.

While writing functions is easy, Kotlin makes it easier for you.

Kotlin has bundled various features with functions that make a developer's life easier. The following is a brief list of the features bundled with Kotlin:

  • Single-expression functions
  • Extension functions
  • Inline functions
  • Infix notation and more

We will cover them in detail in the Lambda, Generics, Recursions, Corecursion section of Chapter 5, More on Functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.239.226