Press enter to see results or esc to cancel. # Lesson 18 – Functions with a single expression in Kotlin Language

()

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)}")
}``````

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
```

[sociallocker id=”958″]

``````fun returnPromedium (v1: Int, v2: Int, v3: Int) = (v1 + v2 + v3) / 3

fun main (parameter: Array <String>) {
print ("Enter first value:")
val value1 = readLine () !!. toInt ()
print ("Enter second value:)
val value2 = readLine () !!. toInt ()
print ("Enter third value:")
val value3 = readLine () !!. toInt ()
println ("Average value of the three numbers entered is \${returnPromedium (value1, value2, value3)}")
}

Project89

fun returnPerimeter (side: Int) = side * 4

fun main (parameter: Array <String>) {
print ("Enter the side of the square:")
val side = readLine () !!. toInt ()
print ("The perimeter is: \${returnPerimeter (side)}")
}

Project90

fun returnSurface (side1: Int, side2: Int) = side1 * side2

fun main (parameter: Array <String>) {
println ("First rectangle")
print ("Enter small side of rectangle:")
val side1 = readLine () !!. toInt ()
print ("Enter largest side of rectangle:")
val side2 = readLine () !!. toInt ()
println ("Second rectangle")
print ("Enter small side of rectangle:")
val side3 = readLine () !!. toInt ()
print ("Enter largest side of rectangle:")
val side4 = readLine () !!. toInt ()
if (returnSurface (side1, side2) == returnSurface (side3, side4))
print ("The two rectangles have the same surface")
else
if (returnSurface (side1, side2)> returnSurface (side3, side4))
print ("The first rectangle has a larger surface")
else
print ("The second rectangle has a larger surface")
}

Project91

fun long (name: String) = name.length

fun main (parameter: Array <String>) {
print ("Enter a name:")
val name1 = readLine () !!
print ("Enter another name:")
val name2 = readLine () !!
if (long (name1) == long (name2))
print ("The names: \$name1 and \$name2 have the same number of characters")
else
if (long (name1)> long (name2))
print ("\$name1 is longer")
else
print ("\$name2 is longer")
}``````

[/sociallocker]

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...