Kotlin Tutorial 7 — Functions

Nick Apperley
4 min readDec 27, 2016

--

Having covered a number of different topics in the tutorial series there is one important topic to go through, functions. Kotlin just like Python has functions but doesn’t have methods. Four types of functions exist in Kotlin: named (ordinary function), lambda, extension, and infix. Atom will be used in this tutorial for all the code samples. For each code sample you will need to save it as a kt (Kotlin Source) file (eg hello.kt). To run a kt file make sure it is selected and press Ctrl+Shift+b. General points to know about functions in Kotlin:

  • If no value is returned from a function then the return type can be omitted (for named functions)
  • An empty function return value is represented by a Unit (different from Python’s None)
  • Functions can be overloaded
  • Any number of parameters can be defined in a function
  • Return type of a function must be specified if the function returns a value (for named functions)
  • Just like Python Kotlin uses the return keyword to return a value from a function (but not with lambdas)
  • Default and named function parameters are supported just like Python
  • Functions can be defined at the top level of a kt or kts (Kotlin Script) file
Illustration 1: Kotlin program compiled in Atom

Named Function

The most basic function type in Kotlin you will encounter very regularly. These functions are called by name and can be passed through as lambdas (more about them later on). To define a named function the keyword fun is used followed by a space and the function’s name, along with the body. Unlike Python Kotlin functions have their body defined in matching curly braces ({}), and don’t use the keyword def. Try the following in Atom:

Illustration 2: Kotlin compiler errors in Atom

What you have seen in the code example above is an entry point (where a program starts) to a Kotlin program. All Kotlin programs require an entry point so any code sample you come across will need one, unless code is being run in the Kotlin REPL or through a kts file. An entry point function in Kotlin is called main and must have exactly one defined parameter that is of type Array<String>. Try this example below in Atom:

Try the following leftPad example in Atom:

If the named function returns a single expression then the return type can be omitted, as seen below:

Lambda

Just like Python Kotlin supports anonymous functions but has a much nicer syntax to use. A lambda is defined by a body ({}). Inside the body if there is only one argument being passed through then the argument is accessed as it. Multiple arguments are defined in the body as comma separated argument names followed by a space and ->. Lambda logic is inserted into the body just after the ->. Try the following in Atom:

If the lambda is being passed through as the last argument to a function then it can be passed outside of the parenthesis, and if the lambda is the only argument then the parenthesis can be dropped. As you can see in the code sample above the greeter function has a single argument called msg that is a function type, where the signature of msg takes zero parameters and returns a String. Below is a modified version of the code sample above:

Variables and constants can hold a reference to a function. Try the following example in Atom below:

Extension Function

A powerful feature in Kotlin that is a great asset when it comes to dealing with software libraries, especially legacy code. An existing data type can be extended with a new built-in function provided the name of the extension function does not match an existing function for the data type. Try the following in Atom:

Note that if the extension function is defined in the same Kotlin source file where it is called then the extension function doesn’t need to be imported.

Conclusion

Although there were a few items to go through you now have some of the basic building blocks in place for splitting up program logic. Next tutorial gets into modularity techniques in Kotlin Tutorial 8 — Modularity.

TOC

  1. Kotlin Tutorial 1 — Introduction
  2. Kotlin Tutorial 2 — Basic Data Types
  3. Kotlin Tutorial 3 — Basic Operators
  4. Kotlin Tutorial 4 — Handling Input And Output
  5. Kotlin Tutorial 5 — Basic Collections
  6. Kotlin Tutorial 6 — Control Flow
  7. Kotlin Tutorial 7 — Functions
  8. Kotlin Tutorial 8 — Modularity
  9. Kotlin Tutorial 9 — Using An IDE
  10. Kotlin Tutorial 10 — Classes
  11. Kotlin Tutorial 11 — Inheritance
  12. Kotlin Tutorial 12 — Encapsulation And Polymorphism
  13. Kotlin Tutorial 13 — Generics

--

--

No responses yet