What is function in Kotlin ?

      No Comments on What is function in Kotlin ?

Functions are defined using the “fun” keyword with optional parameters and a return value. The parameter list must always be present, even if no parameters are defined. For example, this function has no parameters and it returns a
String value:

 fun hello() : String = "hello world"

Each parameter is in the form name: type. The following function accepts two parameters of the type String and also returns a String value:

    fun hello(name: String, location: String): String =
      "hello to you  $name at $location"

If a function does not return any meaningful value, then it is defined to return Unit. As discussed in previous tutoruals, Kotlin Basics, Unit is analogous to the Java and C void types. By using a class that is part of a type hierarchy-rather than a special type, such as void-the type system in Kotlin can be made more regular. Every function must return a value, and this value could be Unit. Functions returning Unit can omit the return type for procedure-style syntax if the developer wishes so. The following two function declarations are equivalent:

 fun print1(str: String): Unit {
      println(str)
    }
    fun print2(str: String) {
      println(str)
    }

Single expression functions

Usually, a function must declare its return type; an exception exists only for functions that consist of a single expression. These are often referred to as one line or single line functions. Such functions can use a shortened syntax that omits the braces and uses the = symbol before the expression rather than the return keyword:

 fun square(k: Int) = k * k

Notice how the function does not need to declare the return value of Int. This is inferred by the compiler. The rationale behind this feature is that very short functions are easy to read, and the return value is a bit of extra noise that doesn’t add much to the overall process. However, you can always include the return value if you think it makes things clearer:

fun square2(k: Int): Int = k * k 

Single expression functions can always be written in regular style if desired. For example, the following two functions are identical and compiled to the same bytecode:

    fun concat1(a: String, b: String) = a + b
    fun concat2(a: String, b: String): String {
      return a + b
    }

Member functions

The first type of functions is called member functions. These functions are defined inside a class, object, or interface. A member function is invoked using the name of the containing class or object instance with a dot, followed by the function name and the arguments in parentheses. For example, to invoke a function called take on an instance of a string, we do the following:

 val string = "hello World !"
    val length = string.take(5)  

Member functions can refer to themselves and they don’t need the instance name to do this. This is because function invocations operate on the current instance, and they are referred to as the following:

     object Rectangle {
      fun printArea(width: Int, height: Int): Unit {
        val area = calculateArea(width, height)
        println("The area is $area")
      }
      fun calculateArea(width: Int, height: Int): Int {
        return width * height
      }
    }  

This code snippet shows two functions that calculate the area of a rectangle and output it to the console. The
printArea function takes two parameters of width and height and uses the calculateArea function to do the math. The first function then outputs the result of the other function.
You will also notice that the calculateArea function uses return, as the value it computes is intended to be used by other functions. The printArea function does not haveany meaningful value to return, so we define its return value as Unit.

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn