Android Application Development All-in-One For Dummies
Book image
Explore Book Buy On Amazon
As an Android app developer, you’ll want to know how to use Kotlin. Kotlin functions provide just what you need. What is a Kotlin function? So glad you asked!

You hear multiple terms for a package of code found in an easily callable form. For example, a procedure is a set of operations executed without calculating any return value. When you program with Java, you hear about the method, because methods are associated with objects. Java is focused on objects, so the most appropriate term is method — a method of working with that object in some meaningful way. In Kotlin, you find functions.

A function is a set of operations that don’t necessarily link to an object but always return a value. If a Kotlin function doesn’t provide a specific return value, it returns a value called Unit. Consequently, you never use the term procedure when working with Kotlin because the procedure doesn’t exist (although you could make the argument that procedures do exist in Java). Here is an example of the Unit return value:

val result = println("A String")

if (result is Unit) { println("result is of type Unit") println(result) }

You wouldn’t expect println() to return a value, and it doesn’t, but it does return Unit. Because println() is a stand-alone call not associated with an object, you always call it a function.

A Minimalistic Android Activity Class

package com.example.myfirstapp

import import android.os.Bundle

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }

Looking at the code above, you see the onCreate() function, which is part of the MainActivity class. Because onCreate() exists as part of a class in this case, you can also call it a method. The onCreate() method exists as a part of objects instantiated from MainActivity. Remember that a function that appears as part of a class is more specifically called a method, even though it’s also a function.

Some ambiguity exists in other languages that use the static method, which is part of a class but is not called as part of an object. Some people argue that these methods are really functions. Kotlin gets rid of the ambiguity by using companion objects in place of static methods in classes. The point is that they really are methods in Kotlin because they’re members of the associated singleton object.

Kotlin functions have certain characteristics, as shown in the following code:

fun main(args: Array) {
    println(monthlyPayment(10_000.00, 5.25, 30))
    println(monthlyPayment(10_000.00, 5.00, 15))

fun monthlyPayment(principle: Double, percentageRate: Double, years: Int): Double { val numPayments: Int = 12 * years val rate: Double = percentageRate / 100.00 val effectiveRate: Double = rate / 12 return (principle * effectiveRate / (1 - Math.pow(1 + effectiveRate, -numPayments.toDouble())))

When you run this example, you receive the expected monthly payments given a 10,000 loan, certain percentage rates, and the number of years that someone will make payments. The functions used in this example have certain characteristics:
  • The names of the two functions are main() and monthlyPayment().
  • In the body of the monthlyPayment() function declaration, the processor computes the monthly payments on a mortgage. You can follow this description of functions and function parameters without understanding anything about the calculations.
  • The body of the monthlyPayment() function uses certain names as placeholders. For example, in the body of the monthlyPayment() function, the name years stands for the number of years in the mortgage's term. Likewise, the name principal stands for the total amount borrowed.
  • Some placeholders appear in parentheses at the beginning of the function’s declaration. The names principal, percentageRate, and years are the function’s parameters. Each parameter is destined to stand for a particular value. But a parameter doesn't stand for a value until an app executes a function call.

The main() function contains a call to monthlyPayment(10_0000.00, 5.25, 30) that gives the function’s first parameter (namely, principal) the value 10000.00. That same call gives the function’s second parameter (percentageRate) the value 5.25. Finally, that function call gives the method's third parameter (years) the value 30.

The next function call in main() gives the monthlyPayment() function’s parameters different values (again 10000.00 for principal, but 5.00 for percentageRate and 15 for years). Each time you call a function, you supply values for the function’s parameters.

  • The types of parameters in a function call must match the types of the parameters in a function declaration. The declaration of function monthlyPayment() has a Double parameter (principal), another Double parameter (percentageRate), and an Int parameter (years). Accordingly, the first function call has two Double parameters (10000.00 and 5.25) followed by an Int parameter (30). The second function call also has two Double parameters followed by an Int parameter.

You can declare the same function more than once, as long as each declaration has a different parameter list. For example, another monthlyPayment() function declaration might have the same name monthlyPayment but only two parameters: principle: Double and percentageRate: Double. To call this alternative monthlyPayment() function, you write something like monthlyPayment(10_000.00, 5.00). In this situation, the body of the alternative monthlyPayment() method probably contains a statement like val years: Int = 30. You don't call this two-parameter method unless you know that the mortgage's term is 30 years.

  • A function call might stand for a value. The first function call in main() stands for the Double value 55.22 (or a value very close to the number 55.22). The value 55.22 comes from all the calculations in the body of the monthlyPayment() function when the principal is 10000.00, the percentageRate is 5.25, and the number of years is 30. Near the end of the monthlyPayment() function body, the formula
principle * effectiveRate / 
(1 - Math.pow(1 + effectiveRate, 

has the value 55.22, and the word return says “send 55.22 back to the statement that called this method.” So, the end of the monthlyPayment() function body effectively says

return 55.22

and the associated println() statement in main() effectively says


Similarly, the second println() function call in main() outputs the value 79.08. Because of the second function call's parameter values, the end of the monthlyPayment() function body effectively says

return 79.08

and the last line in the listing effectively says


A function’s declaration can end with the name of the return type. The monthlyPayment() function declaration begins with the keyword fun (which is short for function), followed by the function name, monthlyPayment, a list of parameters in parentheses, and finally the keyword Double. That's good because the value returned at the end of the method's body (either 55.22 or 79.08) is of type Double.

Want to learn more? Find out why Kotlin is the best choice for developing your Android app.

About This Article

This article is from the book:

About the book authors:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

John Mueller has produced 114 books and more than 600 articles on topics ranging from functional programming techniques to working with Amazon Web Services (AWS). Luca Massaron, a Google Developer Expert (GDE),??interprets big data and transforms it into smart data through simple and effective data mining and machine learning techniques.

This article can be found in the category: