Gathoni's Blog

Gathoni's Blog

Kotlin for Beginners: Getting Started With Functions

Kotlin for Beginners: Getting Started With Functions

How to write User-Defined Functions in Kotlin

Featured on Hashnode

Hey and welcome to my first ever blog on Kotlin!

As part of my #100daysofcode challenge, I have decided to write at least one article each week on something I have learned (fingers crossed I stick with it).

This week I decided to go with Kotlin functions but only because I thought it would be... fun! Pun intended. image.png

In this post, I talk about function basic syntax, functions with return values and functions that receive arguments. And yes, I have examples too.

But first, what are functions?

A function is a group of related code that is written to perform a specific.... well, function or rather task.

Functions can be called over and over and over again. so you basically need to write a function that does something for you once and you can use it multiple times, which is kinda awesome.

There are two types of functions:

1. Kotlin's built-in functions

These are functions that are part of Kotlin's standard library. For instance println() that takes in input like a string and prints on the standard output stream

println("Hey you")

2. User-defined functions.

These are functions that you write yourself.

So how do we write a function in Kotlin?

I have a JavaScript background so I am used to defining the function keyword with literally the word "function", but in Kotlin, things are a bit different.

To define a function, you use fun.

Isn't that fun?... lol

The basic syntax looks like this:

fun hello() {
    // body
}

The above function has four parts:

  • The keyword, 'fun'.
  • The function name, 'hello'.
  • The brackets are where the arguments go.
  • The function body is enclosed with curly brackets.

To make this more useful, I should probably mention the main function.

This is the entry point to a Kotlin program meaning when you execute a program, this is the function that is called first. You have to include it if you want to run your program.

This is what calling the function hello looks like now:

fun main() {
    hello()
}

Let's make the hello function print hello and call it.

fun hello() {
    println("Hello there!")
}
fun main() {
    hello()
}

This is a simple illustration of how it all goes down.

entry poinf.png

You might have noticed that this function doesn't really return anything, it simply prints a greeting.

Functions with return values

Let's make our greeting less generic and personalize it a bit.

fun getName(): String {
    return "Potter"
}

fun hello() {
    var name = getName()
    println("Hello there $name!")
}

fun main() {
    hello()
}

Peep the new function?

So now we have a new function, appropriately named getName() which returns the name of the person we want to greet.

If you are wondering about the : String addition, that is the return type of the function which in our case, is a String.

Alright, how does this program work?

  • main() invokes hello()
  • hello() invokes getName() which returns a name that is stored in the variable name.
  • hello() prints the personalized greeting.

Functions that accept arguments

What if you want to greet someone different each time? You could hard code the name each time into getName() but that would get tiring real fast. A better way would be to use arguments.

fun hello(name: String) {
    println("Hello there $name!")
}

fun main() {
    hello("Potter")
    hello("Ron")
}

This program is more dynamic, you simply pass in any name you want to the hello function and it greets that person.

The output:

Hello there Potter!
Hello there Ron!

In Kotlin, you have to explicitly specify the type of argument a function expects.

hello() receives a name that is a string so the parameter will be name: String and since it doesn't return any value we don't have to include the return type.

Let's Recap

  • A function is a block of related code that performs a specific function.
  • The fun keyword defines a function in Kotlin.
  • The main() function marks the entry point of a Kotlin program.
  • Return types should be specified for functions that return values
  • Arguments data types have to be specified.

This is simply an introduction, there is a whole lot more regarding Kotlin functions. The docs are a good place to start!

Check out this YouTube playlist too. It's really good.

Happy coding :)

 
Share this