HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to KotlinVariables (val / var)Data TypesStringsListsConditions (if / when)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Kotlin›Functions

Functions

Defining functions with fun, parameters, return type, default values, and single-expression bodies.

Simple enough for a 5-year-old to understand

Define and call functions

You bind repetitive work to a name; then you call it with a single line.

Example code
1
fun greet(name: String) {
It takes a string parameter.
FunctionIt takes a string parameter.
2
println("Hello $name!")
Uses the parameter.
Output / InputUses the parameter.
3
}
4
greet("Alex")
Calls the function.
FunctionCalls the function.
FunctionOutput / Input
💡 Hint: It is mandatory to write the parameter type.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.With which keyword are functions defined in Kotlin?

About this topic

Functions in Kotlin are defined with the fun keyword. Parameter types and return types are explicitly written; functions with a single-expression body can use a concise form with an equals sign.

Two of Kotlin's most practical features are default-value parameters and named arguments. Thanks to default values, there's no need to write several different versions of the same function; named arguments make what the call does readable.

Functions that return nothing have a return type of Unit and it doesn't need to be written. It is the equivalent of void in Java, but in Kotlin it is a real type, which makes it possible to pass functions like values.

What you will learn

  • Defining and calling a function
  • Parameter and return types
  • Parameters with default values
  • Named arguments
  • Single-expression function syntax

Who is this for?

  • Those who want to reduce code duplication
  • Those moving from Java and writing overloading
  • Those getting errors in the function signature

When to use?

If a piece of code is written a second time, it should be extracted into a function.

If the same job requires several parameter variations, using parameters with default values instead of writing separate functions produces less code.

Common mistakes

Wrong

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

The function declared that it will return an Int but return was not written; the error "A 'return' expression required in a function with a block body" is received. Printing to the screen and returning a value are different things; the printed value cannot be used in another calculation.

Correct

fun sum(a: Int, b: Int): Int {
  return a + b
}

Wrong

fun increase(value: Int) {
  value = value + 1
}

In Kotlin, parameters are like val and cannot be reassigned; the error "Val cannot be reassigned" is received. This prevents the caller's data from being changed without their knowledge. If you want to produce a new value, you should use a local variable and return the result.

Correct

fun increase(value: Int): Int {
  return value + 1
}

Wrong

fun greet(greeting: String = "Merhaba", name: String) {
  println("$greeting $name")
}
greet("Ayşe")

If a parameter without a default value comes after a parameter with a default value, when you provide only one value in the call, that value goes to the first parameter and the second one is left missing. In this case, Kotlin forces you to use named arguments; the cleanest approach is to put the defaulted parameters at the end.

Correct

fun greet(name: String, greeting: String = "Merhaba") {
  println("$greeting $name")
}
greet("Ayşe")

Frequently asked questions

When is a single-expression function used?

When the body consists of a single expression. Instead of curly braces and return, you write an equals sign; the return type is also usually omitted since it can be inferred. It noticeably simplifies the code in short helper functions.

What is the use of a named argument?

It allows you to explicitly write which value goes to which parameter in the call. It is especially indispensable if there are multiple parameters of the same type or if you want to skip some and provide only one; it also frees up the order.

Can a function be defined inside a function?

Yes, Kotlin allows local functions. You can define a helper that is only meaningful inside that function without moving it outside; it also accesses the variables of the outer function. It is useful for not cluttering the inside of a class with unnecessary helpers.

Summary

Functions are defined with fun.

If a return type is declared, return is mandatory.

Parameters cannot be reassigned.

Parameters with default values should be placed at the end.

Single-expression functions are shortened with an equals sign.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code