Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
kotlin
communitycreator

What are Kotlin functions?

Divine Ugorji

A function is a group of related lines of code that perform a specific task. Functions help to break down complex computer programs into smaller chunks of commands. This makes computer programs easily managed and enables code reusability.

An example of defining a function in Kotlin is as follows:

fun main() {
    println("Hello World!")
}
An example of defining a function in Kotlin

In Kotlin, functions are first-class. This means that they’re independent of classes and can be stored in variables or data structures. They can also be passed as arguments to functions or returned by other functions.

Types of functions in Kotlin

There are two types of functions in Kotlin:

  • Kotlin Standard Library Function: This type of function comes built-in with Kotlin, and is supported out of the box for easy usage. These functions are necessary for the use of the Kotlin programming language.

Some examples of Kotlin standard library functions are as follows:

  • main()
  • print()
  • println()
  • arrayOf()
  • min()
  • max()
  • sqrt()
  • etc.

More examples of Kotlin standard library functions can be found here.

  • User-Defined Functions: The functions the user defines while writing computer programs in the Kotlin programming language.

How to Declare a Function in Kotlin

Illustration of a function in Kotlin
Illustration of a function in Kotlin

Let’s look at the components of a function in Kotlin:

  • The fun keyword is used to define a function in Kotlin.

  • The function name is followed immediately by the fun keyword, which describes the function’s task.

  • The parenthesis is used for the parameters of the function. This could contain none, one, or more parameters of the function.

  • The return type follows immediately after the parameter list. Every function must have a return type. If not explicitly stated, then Kotlin assigns a Unit return type to the function.

  • The opening and closing curly braces are used to contain the function’s body, which implements the specific task of the function.


fun multiply(firstNum : Int, secondNum : Int) : Int {
    return firstNum * secondNum
}

fun main(){
 println("The multiply function would return 6 if numbers 2 and 3 are used as the funtion parameters")
}
An example of a function in Kotlin with two Int parameters and the Int return type

Difference between function and method

  • Functions have an independent existence. Functions can be defined outside of the class, whereas methods depend on objects and are defined within the class.

  • Functions are defined in structured languageslike Pascal, C, and object-based language like JavaScript, whereas methods are found in object-oriented programming languages, such as Java and C#.

  • Functions are self-describing units of code and do not have any reference variables, whereas methods are called using reference variables.

Therefore, while every method is a function, not every function is a method.

RELATED TAGS

function
kotlin
communitycreator
RELATED COURSES

View all Courses

Keep Exploring