Lesson 18 – Functions with a single expression in Kotlin Language

Lesson 18 – Functions with a single expression in Kotlin Language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

Functions with a single expression in Kotlin Language. The functions of a single expression can be expressed in Kotlin without the block of keys and by an assignment indicate the value that it returns.

Recall that one of the goals in Kotlin is to allow us to implement the algorithms in the most concise way possible.

We will solve some of the functions already proposed using this new syntax.

Problem 1

Make a function that we send as a parameter the value of the side of a square and return its surface.

Topic: Functions with a single expression in Kotlin Language

Project85 – Principal.kt

fun returnSurface (side: Int) = side * side

fun main (parameter: Array <String>) {
    print ("Enter the value on the side of the cuff:")
    val la = readLine () !!. toInt ()
    println ("The square surface is ${returnSurface(la)}")
}
Lesson 18 - Functions with a single expression in Kotlin Language - Lesson 18 - Functions with a single expression in Kotlin Language - Lesson 18 - Functions with a single expression in Kotlin Language - Functions with a single expression in Kotlin Language
Functions with a single expression in Kotlin Language

As we can see the complete implementation of the function is a single line:

Fun returnSurface (side: Int) = side * side

We have the operator = and then the expression, in this case the product of the parameter side by itself.

It is not necessary to indicate the type of data that returns the function since the compiler can infer that of the product side * side an int data type is generated.

There is no problem to indicate the type of data to return, but in many situations the compiler can infer as it is this case:

Fun returnSurface (side: Int): Int = side * side

The call to a function containing a single expression does not vary:

    Println ("The square surface is $ {returnSurface}")

Problem 2

Make a function that we send as two integer parameters and return the major one.

Project86 – Home.kt

fun returnMayor (v1: Int, v2: Int) = if (v1> v2) v1 else v2

fun main (parameter: Array <String>) {
    print ("Enter the first value:")
    val value1 = readLine () !!. toInt ()
    print ("Enter the second value:")
    val value2 = readLine () !!. toInt ()
    println ("The major between $value1 and $value2 is ${returnMayor (value1, value2)}")
}

Taking into account that the “if statement” can be arranged as an expression as we saw above is allowed to be used in functions with a single expression:

Fun returnMayor (v1: Int, v2: Int) = if (v1> v2) v1 else v2

Recall that the goal of coding the algorithm with this syntax is to make the code as concise, remember that the other way to express this function is:

fun returnMayor (v1: Int, v2: Int): Int {
    if (v1> v2)
        return v1
    else
        return v2
}

Problem 3

Making a function receives an integer between 1 and 5 and returns in Spanish that number or a String with the string “error” if it is not between 1 and 5.

Project87 – Principal.kt

fun convertCastelano (value: Int) = when (value) {
    1 -> "one"
    2 -> "two"
    3 -> "three"
    4 -> "four"
    5 -> "five"
    else -> "error"
}

fun main (parameter: Array <String>) {
    for (i in 1..6)
        println (convertCastelano (i))
}

In this problem we show that we can use the when statement as the return expression of the function.

Problems proposed

Use a single expression in the requested functions in these problems

  • Develop a function that receives three integers and returns the average value of them.
  • Construct a function that returns the perimeter of a square passing the value of the side as parameters.
  • To make a function that calculates the surface of a rectangle and returns it, the function receives as parameters the values of two of its sides:
    Fun returnSurface (side1: Int, side2: Int): Int
    

    In the main function of the program load the sides of two rectangles and then show which of the two has a larger surface.

  • Make a function that we send as a parameter a String and return the number of characters it has. In the main function ask to load two names by keyboard and call the function twice. Print in the main of the two words has more characters.
Project88

About The Author

Related posts

Leave a Reply