Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Chapter 9. Kotlin Functions

Functions are the building blocks of our apps. We write functions that do specific tasks, and then call them when we need to execute that specific task. As the tasks we need to perform in our apps will be quite varied, our functions need to cater to this and be very flexible. Kotlin functions are very flexible, more so than the other Android-related languages. We therefore need to spend a whole chapter learning about them. Functions are intimately related to object-oriented programming, and once we understand the basics of functions, we will be in a good position to take on the wider learning of object-oriented programming.

This is what we have in store for this chapter:

  • Function basics and recap

  • Function return types and the return keyword

  • Single-expression functions

  • Default arguments

  • More function-related topics

We know a little bit about functions already, so a recap is in order.

Function basics and recap


We have already seen and used functions. Some were provided for us by the Android API, such as onCreate and the other lifecycle functions.

We wrote others ourselves; for example, topClick and bottomClick. However, we haven't explained them properly, and there is more to functions than we have seen so far.

Note

You will often hear another term that is closely related and almost synonymous with functions. This is especially the case if you have previously learned Java or another object-oriented language. The word I am referring to is method. The distinction between a method and a function is rarely important from a technical point of view, and the difference is, in part, where in our code the function/method is declared. If you want to be programmatically correct, you can read this article, which goes into some depth and provides multiple opinions:

https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function

In this book, I will refer...

Making functions flexible


As functions are the building blocks of our code, they need to be versatile to cater for anything we might need to do. We have already seen how we can create very varied parameter lists and return types, as well as deciding in code when to return to the calling code. As we progress, you will see that we need even more options. What follows is a quick glance at some more Kotlin function options that we will introduce now, and then get around to using for real at various points throughout the book.

Default and named arguments

A default parameter is where we the programmers provide a value (default) for a parameter that will be used if the code that calls the function does not provide it. A named argument is when the code calling a function specifies a name along with a value. Note that providing a value is optional. Just because a default value for a parameter is given does not prevent the calling code from overriding it by providing it. Have a look at the following...

Summary


In this chapter, we made good progress with learning about functions. Although functions have been lurking in our code since the first chapter, we finally got to study and understand them formally. We learned about the different parts of a function: the name, the parameters, and the return type. We have seen that what the function actually does goes inside the opening and closing curly brackets, and is called the function body.

We also saw that we can return from a function at any time by using the return keyword, and that we can also use the return type in conjunction with the return keyword to make data from the function available to the code that called the function in the first place.

We learned how we can use default and named arguments to provide different versions of the same function without writing multiple functions. This makes our code more succinct and manageable.

We also discovered that there is even more to functions than we covered in this chapter, but that it is best...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $19.99/month. Cancel anytime

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton