23 Functions: parameters and data return type array

23 Functions: parameters and data return type array

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

We have seen the objective of putting functions into a program and that they can receive data by means of parameters and return a data.

The parameters of a function can be of type Int, Char, Float etc. As we have seen in previous concepts but also can be of type arrangement as we will see in this concept.

Problem 1

Define in the main function an array of integers of 5 elements. To declare two functions, in one to carry the load of its elements and in the other its impression.

Project105 – Principal.kt

fun load (array: IntArray) {
    for (i in array.indices) {
        print ("Enter item:")
        fix [i] = readLine () !!. ToInt ()
    }
}

fun print (fix: IntArray) {
    for (element in array)
        println (element)
}

fun main (parameter: Array <String>) {
    val arre = IntArray (5)
    to charge
    to print
}

In the main function we create an array of 5 integer elements using the IntArray class:

fun main (parameter: Array <String>) {
    val arre = IntArray (5)

We then call the load function and pass the reference to our fix:

    To charge

In the load function we can access the 5 elements of the array to load them:

fun load (array: IntArray) {
    for (i in array.indices) {
        print ("Enter item:")
        fix [i] = readLine () !!. ToInt ()
    }
}

As we can see the parameter is called array and the variable we pass from the main is called arre. There is no problem with having different names but whether it is mandatory that the two are of type IntArray.

The print function receives the array and displays its contents:

fun print (fix: IntArray) {
    for (element in array)
        println (element)
}

Problem 2

You want to store the salaries of operators. When you run the program you must ask for the amount of salaries to enter. Then create an array with that size.
Define a load and print function.

Project106 – Principal.kt

fun load (): IntArray {
    print ("How many salaries do you want to load:")
    val quantity = readLine () !!. toInt ()
    val wages = IntArray (quantity)
    for (i in wages.indices) {
        print ("Enter item:")
        wages [i] = readLine () !!. toInt ()
    }
    return wages
}

fun print (wages: IntArray) {
    println ("List of all salaries")
    for (wages in wages)
    println (wages)
}

fun main (parameter: Array <String>) {
    val wages = load ()
    print (wages)
}

This problem shows how we can create an array in a function and return it. The load function returns the reference of an object of type IntArray:

fun load (): IntArray {

Within the function we create an array, load its contents and finally return it:

    print ("How many salaries do you want to load:")
    val quantity = readLine () !!. ToInt ()
    val wages = IntArray (quantity)
    for (i in salaries.indices) {
        print ("Enter item:")
        wages [i] = readLine () !!. ToInt ()
    }
    return salaries

In the main function call the function load and assign to a variable that by inference is detected that is of type IntArray:

fun main (parameter: Array <String>) {
    val salaries = load ()
    print (salary)
}

Problems proposed

  • Develop a program that allows entering an array of n elements, enter n by keyboard.
    Elaborate two functions one where it is believed and loaded to the array and another that sums all its elements and return that value to the main where it is printed.
  • Load an array of n elements. Print the smallest element and a message if it is repeated within the array.
Project107
fun load (): IntArray {
    print ("How many elements will the array have?")
    val quantity = readLine () !!. toInt ()
    val array = IntArray (quantity)
    for (i in array.indices) {
        print ("Enter item:")
        array [i] = readLine () !!. toInt ()
    }
    return array
}

fun print (array: IntArray) {
    println ("Complete array listing")
    for (element in array)
        println (element)
}

fun sum (array: IntArray): Int {
    var sum = 0
    for (element in array)
        sum += element
                return sum
}

fun main (parameter: Array <String>) {
    var array = load ()
    print (array)
            println ("The sum of all its elements is ${sum (array)}")
}
Project108
fun load (): IntArray {
    print ("How many elements will the array have?")
    val quantity = readLine () !!. toInt ()
    val array = IntArray (quantity)
    for (i in array.indices) {
        print ("Enter item:")
        array [i] = readLine () !!. toInt ()
    }
    return array
}

fun print (array: IntArray) {
    println ("Complete array listing")
    for (element in array)
        println (element)
}

fun major (array: IntArray): Int {
    var may = array [0]
    for (element in array)
        if (element> may)
            may = element
    return may
}

fun repeats (array: IntArray, search: Int): Boolean {
    var cont = 0
    for (element in array)
        if (element == search)
            cont
    if (cont > 1)
    return true
    else
    return false
}

fun main (parameter: Array <String>) {
    var array = load ()
     print (array)
            println ("The major element is ${major(array)}")
    if (repeats(array,major(array)))
    println ("Major is repeated in array")
    else
    println ("Major is not repeated in array")
}

 

About The Author

Related posts

Leave a Reply