0Pricing
Android Academy · Lesson

Declaring Functions

Parameters, return types, and defaults.

Declaring Functions is a free Android Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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()
}

Parameters

A parameter lets you pass data into a function. You write the name, a colon, and the type inside the parentheses.

Below, name: String means the function expects a text value.

fun greet(name: String) {
    println("Hello, " + name + "!")
}

fun main() {
    greet("Ada")
    greet("Sam")
}

Multiple Parameters

A function can take more than one parameter. Separate them with commas.

Here add takes two Int values and prints their sum.

fun add(a: Int, b: Int) {
    println(a + b)
}

fun main() {
    add(3, 4)
}

Returning a Value

Functions can return a result. Write the return type after the parentheses with a colon, then use return.

The caller can store the result in a variable.

fun square(n: Int): Int {
    return n * n
}

fun main() {
    val result = square(5)
    println(result)
}

The Unit Return Type

When a function returns nothing useful, its type is Unit. You usually leave it out.

These two declarations mean the same thing:

  • fun log() { }
  • fun log(): Unit { }

Single-Expression Functions

If a function just returns one expression, you can use the = shorthand and drop the braces and return.

Kotlin can even infer the return type for you.

fun double(x: Int) = x * 2

fun main() {
    println(double(8))
}

Default Parameter Values

You can give a parameter a default value. If the caller omits it, the default is used.

This avoids writing many overloaded versions of the same function.

fun greet(name: String = "friend") {
    println("Hi, " + name)
}

fun main() {
    greet()
    greet("Lee")
}

Named Arguments

When calling a function you can name the arguments. This makes the call clearer and lets you pass them in any order.

Named arguments are handy with default values.

fun box(width: Int = 1, height: Int = 1) {
    println(width * height)
}

fun main() {
    box(height = 5)
}

Functions Calling Functions

One function can call another. This lets you break a big task into small, reusable pieces.

Here main calls average, which calls add.

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

fun average(a: Int, b: Int): Int = add(a, b) / 2

fun main() {
    println(average(10, 20))
}

Why Functions Matter

Functions keep your code organized and DRY (Don't Repeat Yourself).

  • Give a clear name to a piece of logic.
  • Reuse it without copying code.
  • Fix a bug in one place only.

In Android apps you will write functions constantly.

Quick Check

Test your understanding of declaring functions.

Recap

You learned to declare functions with fun, pass parameters, and return values.

You also saw single-expression shorthand, default parameter values, and named arguments. Next you will group data and behavior together using classes.

Frequently asked questions

Is the “Declaring Functions” lesson free?

Yes — the full text of “Declaring Functions” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Declaring Functions”?

Parameters, return types, and defaults. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Declaring Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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