Lesson 17: Functions with data return in Kotlin Language

Lesson 17: Functions with data return 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 data return in Kotlin Language. We saw that a function is defined by a name and that it can receive data by means of its parameters.

Parameters are the way for a function to receive data to be processed. Now we will see another characteristic of the functions that is to return a data to whom invoked the function (remember that a function we can call it from the main function or from another function that we develop)

Topic: Functions with data return in Kotlin Language

Problem 1

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

Project79 – Principal.kt

fun returnSurface (side: Int): Int {
    val sup = side * side
    return sup
}

fun main (parameter: Array <String>) {
    print ("Enter the value on the side of the cuff:")
    val la = readLine () !!. toInt ()
    val surface = returnSurface (la)
    println ("The surface of the square is $surface")
}

 

Lesson 17: Functions with data return in Kotlin Language - Lesson 17: Functions with data return in Kotlin Language - Lesson 17: Functions with data return in Kotlin Language - Functions with data return in Kotlin Language
Functions with data return in Kotlin Language

A new keyword appears in Kotlin to indicate the value returned by the function: return

The function returnSurface receives a parameter named side of type Int. At the end of the declaration of the function we have two points and the type of data that returns the function, in this case, an Int:

Fun returnSurface (side: Int): Int {

We define a local variable called sup where we store the product of the side parameter by itself.

The local variable sup is the one that returns the function using the keyword return:

fun returnSurface (side: Int): Int {
    val sup = side * side
    return sup
}

It should be noted that local variables (in this case sup) can only be queried within the function where they are defined, they are not accessed in the main function or within another function.

Topic: Functions with data return in Kotlin Language

There is an important change when we call or invoke a function that returns a data:

    val surface = returnSurface (la)

That is, the value returned by the returnSurface function is stored in the surface variable.

It is a logical mistake to call the returnSurface function and not assign the value to a variable:

ReturnSurface

The returned data (in our case the surface of the square) is not stored.

If we can use the value returned to pass it to another function:

    print ("The surface of the square is")
    println (returnSurface)

The returnSurface function returns an integer and passes it to the println function to display it.

In Kodlin we can call within a String a function:

    print ("The surface of the square is ${returnSurface}")

We must lock it in braces and precede the $ character (then this is replaced by the value returned by the function)

Problem 2

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

Project80 – Home.kt

fun returnMayor (v1: Int, v2: Int): Int {
    if (v1> v2)
        return v1
    else
        return 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)}")
}

Again we have a function that receives two parameters and returns the largest of them:

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

When a function finds the word return does not continue executing the rest of the function but it leaves to the line of the program from where we call to that function.

Topic: Functions with data return in Kotlin Language

Problem 3

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.

Project81 – Principal.kt

fun long (name: String): Int {
    return 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")
}

We have defined a long called function that receives a parameter named name and returns the number of characters that has that string (we access the length property that has the String class to get the number of characters)

From the main function of our program we call the long function passing the variables name1 and name2:

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

Topic: Functions with data return in Kotlin Language

Problems proposed

  • 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.

Solution

 

About The Author

Related posts

Leave a Reply