Lesson 15: Function parameters in Kotlin language

Lesson 15: Function parameters 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

Function parameters in Kotlin language. We saw in the previous concept that a function solves a part of our algorithm.

On the one hand we have the declaration of the function by means of a name and the algorithm of the function next. Then to run the function we call it from the main function.

Now we will see that a function can have parameters to receive data. Parameters allow us to communicate something to the function and make it more flexible.

Topic: Function parameters 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.

Project74 – Principal.kt

fun showMessage (message: String) {
     println ("*********************************************** ** ")
     println (message)
     println ("*********************************************** ** ")
 }

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

 fun main (parameter: Array <String>) {
     showMessage ("The program calculates the sum of two values ​​entered by keyboard.")
     loadSumar ()
     showMessage ("Thank you for using this program")
 }
Lesson 15: Function parameters in Kotlin language - Lesson 15: Function parameters in Kotlin language - Lesson 15: Function parameters in Kotlin language - Function parameters in Kotlin language
Function parameters in Kotlin language

Now to solve this small problem we have raised a function called showMessage that receives as a parameter a String and displays it on the screen.

Topic: Function parameters in Kotlin language.

The parameters are followed by the name of the function enclosed in parentheses (and in case of having more than one parameter they must be separated by commas):

fun showMessage (message: String) {
     println ("*********************************************** ** ")
     println (message)
     println ("*********************************************** ** ")
 }

A parameter we can imagine as a variable that can only be used within the function.

Now when we call the function showMessage from the main of our program we must pass a String variable or a value of type String:

     ShowMessage ("The program calculates the sum of two values ​​entered by keyboard.")

The string we passed: “The program calculates the sum of two values entered by keyboard.” It receives the parameter of the function.

A function with parameters makes us more flexible to use it in different circumstances. In our problem the showMessage function is used for both the initial presentation of our program and to show the farewell message. If there were no parameters we would be forced to implement two functions as the previous concept.

Topic: Function parameters in Kotlin language.

Problem 2

Make a function that receives three integers and shows us the largest of them. Load the values do it by keyboard in the main function.

Project75 – Main.kt

fun showMayor (v1: Int, v2: Int, v3: Int) {
     print ("Mayor:")
    if (v1> v2 && v1> v3)
         println (v1)
     else
         if (v2> v3)
             print (v2)
         else
             print (v3)
 }

 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 ()
     showMayor (value1, value2, value3)
 }

It is important to note that a program in Kotlin does not run in a linear way the functions defined in the * .kt file but it starts in the main function.

In the main function, it is requested to enter three integers per keyboard and calls the function ShowMayor and passes its parameters to the three integer variables value1, value2 and value3.

The showMayor function receives in its parameters v1, v2 and v3 the values loaded in the variables value1, value2 and value3.

The parameters are the form that allows us to communicate the main function with the showMayor function.

Within the showMayor function we can not access the variables value1, value2 and value3 since they are local variables of the main function.

Topic: Function parameters in Kotlin language.

Problem 3

Develop a program that allows you to enter the side of a square. Then ask if you want to calculate and show its perimeter or its surface.

Project76 – Principal.kt

fun showPerimeter (side: Int) {
     val perimeter = side * 4
     println ("Perimeter is $perimeter")
 }

 fun showSurface (side: Int) {
     val surface = side * side
     println ("Surface is $surface")
 }

 fun main (parameter: Array <String>) {
     print ("Enter the value of the side of a square:")
     val la = readLine () !!. toInt ()
     print ("You want to calculate the perimeter or surface [enter text: perimeter / surface]")
     var answer = readLine () !!
     when (answer){
         "Perimeter" -> showPerimeter (la)
         "Surface" -> showSurface(la)
     }
 }

We define two functions that calculate and show the perimeter on one side and on the other the surface:

fun showPerimeter (side: Int) {
     val perimeter = side * 4
     println ("Perimeter is $perimeter")
 }

 fun showSurface (side: Int) {
     val surface = side * side
     println ("Surface is $surface")
 }

In the main function we load the side of the square and enter a String that indicates which calculation we want to perform if we obtain the perimeter or the surface. Once the response variable is entered, we call the function that performs the respective calculation, passing the local variable “la” which stores the value of the square side as the data.

Parameters are the fundamental tool for passing data when we call a function.

Topic: Function parameters in Kotlin language.

Problems proposed

  • In the main function ask to enter a key twice per keyboard.
    Develop a function that receives two String as parameters and displays a message if the two keys entered are the same or different.
  • Make a function that receives three integers and shows them sorted from least to greatest. In the main function ask to load 3 integers per keyboard and proceed to call the first defined function.

Solution

 Project77.kt

About The Author

Related posts

Leave a Reply