Syntax

Kotlin Define a Function Syntax

In Kotlin, define a function: defines a function — fun add(a: Int, b: Int): Int = a + b

Quick Answer

Syntax
fun add(a: Int, b: Int): Int = a + b
KotlinSyntax

Define a Function

Code example
fun add(a: Int, b: Int): Int = a + b

Defines a function.

Learn More

How to define a function in Kotlin

  1. Open your Kotlin file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Kotlin Define a Function Syntax

Use fun add(a: Int, b: Int): Int = a + b.

Related Syntax References

Frequently asked questions

What is the Kotlin syntax for define a function?

Use this pattern: fun add(a: Int, b: Int): Int = a + b

Can I copy this Kotlin code example?

Yes. Use Copy code and adapt the example to your program.

Is this Kotlin syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify