bekwam courses

Kotlin for Students #003 - Functions Challenge

August 28, 2017

This article presents a solution to a challenge issued in the previous article 002 Kotlin Functions. In that article, I presented how to create and use Kotlin Functions to calculate the area of a rectangle. I ended the article with a suggestion to Go Further and define your own function using the familar formula to compute the volume of a box (a rectangular prism).

The formula to compute the volume of a box is Volume = length * width * height. As with the computeArea function, the return value will be a single multiplication operation. However, the new volume computation involves three parameters (length, width, height) rather than the two parameters of the area computation (just length and width).

The following functions show the computeArea function from the previous article and a new computeVolume function from the challenge.

fun computeArea(length : Double, width : Double) : Double {
    return length * width
}

fun computeVolume(length : Double, width : Double, height : Double) : Double {
    return length * width * height
}

computeVolume is the name of the new function. This is just a name that I picked. It could have been "calculateVolume" or "determineVolume" or even a name that doesn't mention "volume" at all. However, it's best to name your functions as descriptively as possible.

length : Double, width : Double, height : Double is the list of three parameters provided to the computeVolume function. The : Double at the end of the function definition, before the opening curly brace indicates a return type of Double.

Println

We'll use the println function packed with a String Template to output and testing our new function. This wraps a call to the computeVolume function in the special String Template syntax "${}".

fun main(args: Array<String>) : Unit {
    println("area of 6.5x8.1 rectangle: ${computeArea(6.5,8.1)}")    
    println("area of 7.1x8.2x9.3 rectangle: ${computeVolume(7.1,8.2,8.3)}")
}

Running the Program

This screenshot shows the two function definitions for computeArea and computeVolume. computeArea is from the first article and computeVolume should mirror what you've done for the challenge. It's perfectly fine to have a different function name, one other than "computeVolume". You're also able to use different names for the parameter. For example, you can use (l,w,h) instead of (length,width,height).

Screenshot of try.kotlinlang.org
Testing the computeVolume Function

Break Something

To get familiar with functions, make small changes to the function or parameter names. For example, change computeVolume to calculateVolume and re-run. Notice that you can't remove the name entirely or you'll get an error (go ahead and try it).

Try substituting your own values in the println. Instead of 7.1, put 6.1. But be careful, you might get something like this!

Screenshot of try.kotlinlang.org
A Simple Volume with a Bunch of Nines

What's with all the extra nines? This is a limitation of computers and the imprecise way in which they represent numbers. If you tried your own numbers and are frustrated to see this, read below for a way to fix this. I'll discuss numbers and precision in a later article.

For now, replace the println in the main with the following. Notice the .toFloat() that's been appended to the end of computeVolume.

	println("area of 7.1x8.2x9.3 rectangle: ${computeVolume(7.1,8.2,9.3).toFloat()}")    

Hopefully, this article confirmed your challenge response. If not, it gave you another look at a slightly different function.


Headshot of Carl Walker

By Carl Walker

President and Principal Consultant of Bekwam, Inc