Press enter to see results or esc to cancel.

Lesson 16: Concept of functions in Kotlin language

()

Concept of functions in Kotlin language. So far we have worked solving the whole problem in the main function proposed in Kotlin.

This way of organizing a program can only be carried out if it is very small (tens of lines).

Topic: Concept of functions in Kotlin language.

Now we will try to divide or decompose a complex problem into small problems. The solution of each of these small problems brings us the solution of the complex problem.

In Kotlin the raising of these small solutions to the complex problem is done by dividing the program into functions.
A function is a set of instructions in Kotlin that solve a specific problem.

Let’s see how to create our own functions.

The first problems that we present we may seem to be more convenient to solve everything in the main function instead of dividing it into small functions. As we progress we will see that if a program starts to be more complex (hundreds of lines, thousands of lines or more) the division into small functions will allow us to have a program more orderly and easy to understand and therefore to maintain.

Topic: Concept of functions in Kotlin language.

Problem 1

Make an application that shows an on-screen presentation of the program. Request the loading of two values and show us the sum. Finally show a farewell message of the program.
Implement these activities in three functions.

Project70 – Principal.kt

``` fun presentation () {
println ("Returns the sum of the values")
println ("*******************************")
}

print ("Enter the first value:")
val value1 = readLine () !!. toInt ()
print ("Enter the second value:")
val value2 = readLine () !!. toInt ()
val sum = value1 + value2
println ("The sum of the two values ​​is: \$sum")
}

fun finalization () {
println ("*******************************")
println ("Thank you for using this program")
}

fun main (parameter: Array <String>) {
presentation()
ending()
}
```

The way we organize our program changes radically.
The program in Kotlin always starts in the main function.

In other words, the program starts at:

``` fun main (parameter: Array <String>) {
presentation()
ending()
}
```

As we can see in the main function we call the three functions we declared previously.

The syntax for declaring a function is by using the fun keyword followed by the function name (the function name can not have any blanks or start with a number)

After the name of the function must enter in parentheses the data that arrive, if no data arrives as is the case of our three functions only open and closed parentheses.

Topic: Concept of functions in Kotlin language.

The entire block of the function is enclosed in braces and indented four spaces as we have been working with the function main.

Within a function we implement the algorithm that we intend to solve that function, for example the presentation function aims to display the program objective:

``` fun presentation () {
println ("Returns the sum of the values")
println ("*******************************")
}
```

The function loadSumar allows to enter two integers by keyboard, to add them and to display them in the screen:

``` fun loadSumar () {
print ("Enter the first value:")
val value1 = readLine () !!. toInt ()
print ("Enter the second value:")
val value2 = readLine () !!. toInt ()
val sum = value1 + value2
println ("The sum of the two values ​​is: \$sum")
}
```

The end function () is intended to display a message informing the operator that the program has ended:

``` fun finalization () {
println ("*******************************")
println ("Thank you for using this program")
}
```

After defining the functions we have at the end of our file Principal.kt calls the functions inside the main function:

``` fun main (parameter: Array <String>) {
presentation()
ending()
}
```

If we do not make the functions calls the algorithms that implement the functions will never run.

When in the main program block a function is called until it does not end with the call to the following function:

In Kotlin the main function can actually be defined at the beginning of the file and then the other functions, which is important to say that a program in Kotlin always starts running in the main function.

Topic: Concept of functions in Kotlin language.

Problem 2

Make an application that requests the loading of two integer values and show its sum.
Show a separator line after each time we load two values and their sum.

Project71 – Principal.kt

``````fun loadSuma () {
print ("Enter the first value:")
val value1 = readLine () !!. toInt ()
print ("Enter the second value:")
val value2 = readLine () !!. toInt ()
val sum = value1 + value2
println ("The sum of the two values ​​is: \$sum")
}

fun separation () {
println ("*******************************")
}

fun main (parameter: Array <String>) {
for (i in 1..5) {
separation()
}
}``````

We have declared two functions, one that allows loading two integers to add them and show the result:

``````fun loadSuma () {
print ("Enter the first value:")
val value1 = readLine () !!. toInt ()
print ("Enter the second value:")
val value2 = readLine () !!. toInt ()
val sum = value1 + value2
println ("The sum of the two values ​​is: \$sum")
}``````

And another function that aims to show a separator line with asterisks:

``````fun separation () {
println ("*******************************")
}``````

From the main function we call the functions loadSumar and separation 5 times:

``````fun main (parameter: Array <String>) {
for (i in 1..5) {
separation()
}
}``````

The new thing that should be clear is that the call to functions from the main function of our program can be done multiple times.

Topic: Concept of functions in Kotlin language.

Problems proposed

• Develop a program with two functions. The first request the input of an integer and show the square of that value. The second that requests the loading of two values and show the product of them. Call from main to both functions.
• Develop a function that requests the load of three values and show the lowest. From the main function of the program call 2 times to said function (without using a repetitive structure)

Solution

[sociallocker id=”958″]

` Project72`
``````fun calculatedSqure () {
print ("Enter an integer:")
val value = readLine () !!. toInt ()
val  square  = value * value
println ("The square is \$square")
}

fun calculateProduct () {
print ("Enter first value:")
val value1 = readLine () !!. toInt ()
print ("Enter second value:")
val value2 = readLine () !!. toInt ()
val product = value1 * value2
println ("The product of values ​​is: \$product")
}

fun main (parameter: Array <String>) {
calculatedSqure ()
calculateProduct ()
}``````
` Project73`
``````fun lowestValue () {
print ("Enter first value:")
val value1 = readLine () !!. toInt ()
print ("Enter second value:")
val value2 = readLine () !!. toInt ()
print ("Enter third value:")
val value3 = readLine () !!. toInt ()
print ("Minor of the three:")
when {
value1 <value2 && value1 <value3 -> println (value1)
value2 <value3 -> println (value2)
else -> println (value3)
}
}

fun main (parameter: Array <String>) {
lowestValue ()
lowestValue ()
}``````

[/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...