Functions with a single expression. Unit type. Functions that return the Unit type. Examples
Contents
- 1. General concepts about using functions with a single expression
- 2. Examples of functions with a single expression
- 3. An example of using a function with a single expression combined with the when statement. Calculate the number of days in a month
- 4. An example of using a function with a single expression in combination with the if statement
- 5. The Unit type. Functions that return the Unit type
- 6. Examples of functions that return the Unit type
- Related topics
Search other resources:
1. General concepts about using functions with a single expression
A function body can contain any number of operators. However, there are cases when a function contains one expression (one line). If a function contains one expression, then in the Kotlin language it can be styled in a special way. The general form of a function declaration with a single expression is as follows
fun FuncName(parameters) = expression
here
- FuncName – the name of function;
- parameters – list of function parameters;
- expression – an expression that defines the body of the function.
If several expressions (operators) need to be executed in the body of a function, then the usual syntax for declaring a function is used.
⇑
2. Examples of functions with a single expression
Example 1. A function that calculates the modulus of a complex number. The function receives as parameters the real (re) and imaginary (im) parts of a complex number.
// A function that calculates the modulus of a complex number fun AbsComplex(re:Double, im:Double) = Math.sqrt(re*re+im*im) fun main(args:Array<String>) { // Demonstration of using the AbsComplex() function var module : Double var re : Double var im : Double print("re = ") re = readLine().toString().toDouble() print("im = ") im = readLine().toString().toDouble() module = AbsComplex(re,im) print("module = " + module) }
Test example
re = 3 im = 4 module = 5.0
Example 2. A function that calculates the volume of a sphere based on a given radius.
// A function that determines the volume of a sphere by a given radius fun Volume(radius:Double) = 4.0/3 * Math.PI * Math.pow(radius, 3.0) fun main(args:Array<String>) { // Demonstration of using the Volume() function var volume : Double var radius : Double print("radius = ") radius = readLine().toString().toDouble() volume = Volume(radius) println("volume = " + volume) }
Test example
radius = 2.5 volume = 65.44984694978736
⇑
3. An example of using a function with a single expression combined with the when statement. Calculate the number of days in a month
Task.
Develop a function that determines the number of days in this month by the month number (1..12) and year number.
Solution.
// Function that determines the number of days in a month fun GetNDays(month : Int, year : Int) = when (month) { 4, 6, 9, 11 -> 30 1, 3, 5, 7, 8, 10, 12 -> 31 2 -> { if (year % 400 == 0) 29 else if (year % 100 == 0) 28 else if (year % 4 == 0) 29 else 28 } else -> -1 } fun main(args:Array<String>) { // Demonstration of using the GetNDays() function // 1. Input data print("year = ") val year = readLine().toString().toInt() print("month = ") val month = readLine().toString().toInt() // 2. Calling the GetNDays() function val nDays = GetNDays(month, year) // 3. Display the result if (nDays==-1) println("Incorrect input.") else println("days = " + nDays) }
Test example
year = 2008 month = 2 days = 29
⇑
4. An example of using a function with a single expression in combination with the if statement
Task. A positive number between 0 and 999999 is specified. Develop a function with a single expression that calculates the number of digits in a number.
Solution.
// Function that returns the number of digits in a number fun GetDigits(number : Int) = if (number < 0) -1 else if (number<10) 1 else if (number<100) 2 else if (number<1000) 3 else if (number<10000) 4 else if (number<100000) 5 else -1 fun main(args:Array<String>) { // Demonstration of using the GetDigits() function // 1. Input number print("number = ") val number = readLine().toString().toInt() // 2. Invoke the GetDigits() function val nDigits = GetDigits(number) // 3. Display the result if (nDigits==-1) println("Incorrect input") else println("nDigits = " + nDigits) }
Test example
number = 5564 nDigits = 4
⇑
5. The Unit type. Functions that return the Unit type
If a function does not return a value, then in Kotlin this function is defined such that it returns the Unit type. If the return keyword is not used in the function body, then the function is considered to return a value of the Unit type.
In Kotlin, the Unit type indicates a function that does not return anything, but can be applied to generic functions. Returning at least some type is required in generic functions.
In the most general case, a function that returns the Unit type has the form
fun FuncName(parameters) : Unit { // ... }
here
- FuncName – the name of function;
- parameters – function parameters.
⇑
6. Examples of functions that return the Unit type
6.1. Display the binary representation of a decimal number
Task. Design a function that takes an integer and displays the binary representation of that number.
Solution.
// A function that returns the Unit type. // A function that outputs the binary equivalent of an integer. fun Print_10_to_2(number : Int) : Unit { var t = number var s = "" while (t>0) { s = (t%2).toString() + s t = t/2 } println(number.toString() + " => " + s) } fun main(args:Array<String>) { // Demonstration of using the Print_10_to_2() function // 1. Input a number print("number = ") val number = readLine().toString().toInt() // 2. Invoke the function Print_10_to_2(number) }
Test example
number = 78 78 => 1001110
⇑
6.2. A function that prints a number in reverse order (1234 => 4321)
Task. Develop a function that prints an integer in reverse order.
Solution.
// A function that returns the Unit type. // The function displays the number in reverse order fun Print_Reverse_Number(number : Int) : Unit { var t = number var s = "" while (t>0) { s += (t%10).toString() t = t/10 } println(number.toString() + " => " + s) } fun main(args:Array<String>) { // Demonstration of using the Print_Reverse_Number() function // 1. Input number print("number = ") val number = readLine().toString().toInt() // 2. Invoke the function Print_Reverse_Number(number) }
Test example
number = 1234 1234 => 4321
⇑
Related topics
- Functions. The fun keyword. Refactoring a function. The return statement
- Function scope. Local scope. Default arguments in function parameters
⇑