Your First Kotlin Android App: An App From Scratch

Jul 5 2022 Kotlin 1.6, Android 12, Android Studio Bumblebee | 2021.1.1

Part 3: Coding in Kotlin

21. Write Another Type of Function

Episode complete

Play next episode

Save for later
About this episode
See versions

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 20. Understand Kotlin's Standard Library Next episode: 22. Challenge: Calculate the Difference

This video Write Another Type of Function was last updated on Jul 5 2022

Currently, the BullsEye app generates a target value for the player to aim for. The player can then move the slider as close as they can to the target value then tap the “Hit Me” button.

This triggers the button’s action to display the alert dialog with the sider’s value. All that’s left is to find a way to calculate the player’s score.

We need to create a self-contained action “calculate score.” Now, where do we put the code to calculate the score?

Rememeber, actions in kotlin are represented as functions. So in this case, we put the code inside a new function.

We’ve used functions that have parameters ie when we call them, we pass in some inputs known as arguments. This time we will be creating a type of function that returns an output otherwise known as a return value.

To do that, you simply create a function like this that adds a type after the parenthesis in the funciton definition. The colon here introduces the type.

You may have seen this colon syntax in the main activity. It is used to define a type. So a function that returns an integer has a return type of Integer.

Then inside the curly braces, you add the code that forms the body of the function. This will be the logic that results in the final return value. Then as the last line of code in the function you return the value using the return keyword followed by the value.

The value must match the return type of the function. So if the method definition says it returns an Integer value then you can return stuff like 2, 52, 200, or any other integer values. Note: the return value must be the last line of the function, otherwise you’ll encounter an error.

Now we’re going to create a new function just above the showResult() function. Enter the following code:

private fun pointsForCurrentRound(): Int {
    return 999

You can see it returns an interger value 999 for now. And this is because of the return type of the function in the declaration.

This function will return a value that will be displayed as part of the dialog message. Let’s head over to the strings.xml file. The result_dialog_message string currently has one placeholder for a value to the passed in. Let’s add the remaining string and the second placeholder for the score.

Update the string to the following:

<string name="result_dialog_message">The slider\'s value is %1$d.\nYou scored %2$d points this round.</string>

Inside the updated string, you added \n which is a newline character. This will push the text after it to the next line.

Then you added the rest of the message text to the string and placed a second placeholder inside it. This is denoted with the number 2.

Go back to the main activity file. As you can see, the code inside the showResult function is already showing an error because it is now expecting a second argument. To solve this, you need to pass in the call to the function you just created as the second argument of the getString function for the dialogMessage.

Do that now:

val dialogMessage = getString(R.string.result_dialog_message, sliderValue, pointsForCurrentRound())

If your code is becoming too long and crossing the vertical guide line. You go to the menu above.

Then you click on “Code”. Then select “Reformat Code”

This helps you format your code to make it more readable. In this case, it brings the call to getString() to the next line.

Run your app once again to try it out.

You can see the score returned from the new function we just created is displayed correctly as a message in the dialog.