Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Functions | Kotlin Basics
Android Development with Kotlin
course content

Contenido del Curso

Android Development with Kotlin

Android Development with Kotlin

1. Kotlin Basics
2. Control Flow and Collections
3. Object-Oriented Programming in Kotlin

bookFunctions

Let's talk about functions in more detail.

In previous chapters, we frequently encountered the keyword fun, which designates a function when we used the main function. In this chapter, we'll explore what functions are, and learn how to create and use them.

You are already familiar with the main function, but implementing other functions can be slightly different.

Let's consider a situation where we want to call a function by providing our name, and it will print a greeting with the given name.

Such a function might look like this:

kt

Main

copy
123
fun greeting(name: String) { println("Hello, $name! Welcome to our Kotlin application!") }

Let's break down what's written in this function step by step.

It all starts with the keyword fun, followed by the function name, which in our case is greeting. After that, we open parentheses where we specify parameters for the function. This is optional, and simply put, parameters are what we pass and use within this function.

For example, we use a parameter name with the data type String, because we want to pass the user's name into the function, and then display it in the console.

Next, we open the function body and write the operation we want to perform, which in our case is printing a greeting to the console.

The main advantage of using functions is that we can reuse them as many times as needed.

For example:

kt

Main

copy
12345678910111213
fun main() { val name1: String = "Daniel" val name2: String = "Alex" val name3: String = "John" greeting(name1) greeting(name2) greeting(name3) } fun greeting(name: String) { println("Hello, $name! Welcome to our Kotlin application!") }

Here, you might not see the main efficiency because the function has code in just one line, but functions are invaluable when it's about more complex logic or operations, like sorting a large dataset.

Returning Values from the Function

Functions can also return values; for this, you need to specify a return type, for example:

kt

Main

copy
1234567891011
fun main() { val firstSum: Int = sum(10, 15) val secondSum: Int = sum(firstSum, 30) val thirdSum: Int = sum(firstSum, secondSum) print("Result of the first sum is $firstSum, for the second sum is $secondSum, and $thirdSum for the third one!") } fun sum(firstNumber: Int, secondNumber: Int) : Int { return firstNumber + secondNumber }

Here, we've written a simple addition function just as an example. As you can see, it takes 2 parameters: firstNumber and secondNumber. Ultimately, we declare the data type that will be returned from this function, which is Int in our case.

Inside the function body, we use the keyword return to specify what will be returned from the function.

The general syntax for writing functions looks like this:

We can write the complex or simple logic in functions and then use them by simply calling them by name!

In the next chapter, we will look at more complex and interesting logic with functions and practice creating them!

bla bla bla

bla bla bla

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 5
some-alt