Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Expression Body

If a function’s body can be written as a single expression then you can omit the braces and return statement. In this style of function definition, you must use an = symbol between the function header and the expression. There is typically no need to specify the return type explicitly, as the compiler can usually infer this.

For example, here’s how you could write a Kotlin function to compute the area of a circle:

import kotlin.math.PI

fun circleArea(radius: Double) = PI * radius * radius

[Run this function]

Notice the absence of an explicit return type. The compiler will infer the return type to be Double, because the expression is a multiplication of values that are all of type Double.

Notice also the absence of braces or the return keyword.

As another example, consider the when expression to compute exam grades that we saw earlier. You could make this code more reusable by turning it into a function with an expression body. Here’s what such a function could look like:

fun grade(mark: Int) = when (mark) {
    in 0..39   -> "Fail"
    in 40..69  -> "Pass"
    in 70..100 -> "Distinction"
    else       -> "?"
}

[Run this function]

If you wanted, you could write this with a block body instead:

fun grade(mark: Int): String {
    when (mark) {
        in 0..39   -> return "Fail"
        in 40..69  -> return "Pass"
        in 70..100 -> return "Distinction"
        else       -> return "?"
    }
}

However, the version with an expression body is neater and more compact.

Task 5.2.1

  1. Edit the file named Circle.kt, in the tasks/task5_2_1 subdirectory of your repository. Copy the circleArea() implementation shown above into this file.

  2. Add a new function, similar to circleArea(), that computes and returns the perimeter (circumference) of a circle. Use an expression body for this new function, like circleArea() does.

  3. Add a third function named readDouble(). This function should have a single String parameter, representing a prompt that will be displayed to the user. After printing this prompt, the function should read input from the user, convert it to a Double value and then return this value.

  4. Finally, add a main() function that uses all three of these functions. Your finished program should read a value for circle radius from the user, compute area and perimeter, then display these quantities to four decimal places.

    Hint

    See the earlier discussion of formatted output if you need help with displaying area and perimeter in the required way.

  5. Compile your program, then run it a few times to check it behaves as expected.

Task 5.2.2

  1. Edit the file named Grades.kt, in the tasks/task5_2_2 subdirectory of your repository. Copy the expression form of the grade() function shown above into this file.

  2. Next, add to the file a main() function that

    • Iterates over the program’s command line arguments using a for loop
    • Converts each argument to an Int, representing an exam mark
    • Invokes grade() on the exam mark, to determine the corresponding grade
    • Prints both the mark and the grade
  3. Compile your program, then test it by running it with different numeric arguments. Here’s an example of how you might run it from the command line, and what the output might look like:

    $ kotlin GradesKt 45 72 29 63
    45 is a Pass
    72 is a Distinction
    29 is a Fail
    63 is a Pass