Checkout the functions in kotlin example

WHAT IS KOTLIN?

Kotlin is a general-purpose, statically typed, and open-source programming language. It runs on JVM and can be used anywhere Java is used today. It can be used to develop Android apps, server-side apps and much more. Google accepetd the kotlin as the officill language for android.

HISTORY OF KOTLIN

Kotlin was developed by JetBrains team. A project was started in 2010 to develop the language and officially, first released in February 2016. Kotlin was developed under the Apache 2.0 license.

FEATURES OF KOTLIN

  • Concise: Kotlin reduces writing the extra codes. This makes Kotlin more concise.
  • Null safety: Kotlin is null safety language. Kotlin aimed to eliminate the NullPointerException (null reference) from the code.Interoperable.
  • Interoperable: Kotlin easily calls the Java code in a natural way as well as Kotlin code can be used by Java.
  • Smart cast: It explicitly typecasts the immutable values and inserts the value in its safe cast automatically.
  • Compilation Time: It has better performance and fast compilation time.
  • Tool-friendly: Kotlin programs are build using the command line as well as any of Java IDE.
  • Extension function: Kotlin supports extension functions and extension properties which means it helps to extend the functionality of classes without touching their code.

Functions in Kotlin

Function is a group of inter related block of code which performs a specific task. Function is used to break a program into different sub module. It makes reusability of code and makes program more manageable.

Kotlin, functions are declared using fun keyword. There are two types of functions depending on whether it is available in standard library or defined by user.

  • Standard library function
  • User defined function

Standard Library Function

Kotlin Standard library function is built-in library functions which are implicitly present in library and available for use.

  1. fun main(args: Array){
  2. var number = 25
  3. var result = Math.sqrt(number.toDouble())
  4. print(“Square root of $number is $result”)
  5. }
  • Here, sqrt() is a library function which returns square root of a number (Double value).
  • print() library function which prints a message to standard output stream.

User defined Function

User defined function is a function which is created by user. User defined function takes the parameter(s), perform an action and return the result of that action as a value.

  1. fun functionName(){
  2. // body of function
  3. }
  1. functionName()

Kotlin simple function example

  1. fun main(args: Array){
  2. sum()
  3. print(“code after sum”)
  4. }
  5. fun sum(){
  6. var num1 =5
  7. var num2 = 6
  8. println(“sum = “+(num1+num2))
  9. }

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By : XYZScripts.com