avatarGabriel Shanahan

Summary

The provided web content offers an overview of Kotlin's basic function features, emphasizing its advantages over Java, such as the ability to define functions outside of classes, postfix type declaration, default argument values, and the use of Unit instead of void.

Abstract

Kotlin's approach to functions is presented as more flexible and user-friendly compared to Java. Functions in Kotlin can be declared outside of classes, which is not possible in Java. The syntax for defining functions in Kotlin places the return type after the function name, aligning the names visually and enhancing readability. Additionally, Kotlin functions can have default values for parameters, and these values can be complex expressions. Unlike Java's void, Kotlin uses Unit as a return type for functions that do not return a value, treating it as a proper type that can be checked. The content also touches on the concise syntax for single-expression functions, the use of named arguments, and the existence of more advanced topics like varargs and infix functions, which are to be covered in subsequent sections. The article is part of a larger series called "The Kotlin Primer," aimed at facilitating Kotlin adoption in Java-centric organizations.

Opinions

  • The author expresses that Kotlin functions are more enjoyable to work with than Java functions due to their increased flexibility and expressiveness.
  • The article suggests that the Kotlin language's design choices for function declaration and usage improve code readability and facilitate organizational learning.
  • The author conveys gratitude towards Etnetera a.s. for supporting the creation of the Kotlin Primer series, indicating a collaborative relationship.
  • The recommendation to read the introduction and the presence of a table of contents imply a structured and thoughtful approach to learning Kotlin.
  • The use of interactive Kotlin Playground examples within the article demonstrates a preference for hands-on learning and immediate practical application.

Basic Functions

A brief rundown of functions in Kotlin

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Functions are fun and are therefore declared with the fun keyword, followed by the argument list.

Kotlin functions are much more fun than Java functions:

  • Unlike Java, function declarations can live outside classes
  • Unlike Java, types are specified after declarations. Visually, this nicely aligns function names into the same column.
  • Unlike Java, arguments can have default values. Those values can be expressions.

Semicolons are optional (and never used in practice). Functions which are only called for their side effects (i.e. which do not return a value) return the type Unit.

Unit is similar to void in Java, except it is an actual type:

  • For example, you can write if(x is Unit) in Kotlin, but you cannot write if(x instanceof void) in Java
  • More on this in the lesson on Types, don’t pay too much attention to it for now

Functions which consist of a single expression can be written as such:

Kotlin supports named arguments:

Much more information can be found in the official docs, and we will cover related topics (e.g. varargs, infix functions etc.) later.

Go back to the Preface, jump to the Table of Contents, or continue to Basic Variables.

Join me in Etnetera
Kotlin
Java
Learning
Function
Programming
Recommended from ReadMedium