0Pricing
Android Academy · Lesson

Declaring Functions

Parameters, return types, and defaults.

What Is a Function?

A function is a named block of code you can run again and again. Instead of repeating lines, you call the function by its name.

In Kotlin you declare a function with the fun keyword, followed by a name and a pair of parentheses.

Your First Function

Here is a simple function called greet. The body lives inside the curly braces { }.

We call it from main by writing its name with parentheses: greet().

fun greet() {
    println("Hello, Kotlin!")
}

fun main() {
    greet()
}

All lessons in this course

  1. Declaring Functions
  2. Classes and Properties
  3. Constructors and init
  4. Data Classes
← Back to Android Academy