Press enter to see results or esc to cancel.

# Lesson 43 – Array (more than one) values as parameters in kotlin Function/Method

##### Share this
()
Table of Contents
1. Spread operator

In the Kotlin language, a method of a class or function can receive a variable amount of parameters using the keyword “vararg” prior to the parameter name.

From where we call the function we pass a list of values and receive a single parameter by grouping said data into an array.

```Fun print (vararg names: String) {
For (element in names)
Print ("\$element")
Println()
}

Fun main (args: Array <String>) {
Print ("Juan", "Ana", "Luis")
}
```

As we can see we use vararg before the name of the parameter and the type of data that will store the array:

```Fun print (vararg names: String) {
For (element in names)
Print ("\$element")
Println ()
}
```

Within the function, we treat the parameter names as an array.

When we call the print function we do not send an array but a list of String, the compiler is responsible for transforming that list into an array.

Problem 1

Construct a function that receives a variable quantity of integers and returns its sum.

Project 170

``````fun sum (vararg numbers: Int): Int {
var sum = 0
for (element in numbers)
sum += element
return sum
}

fun main (args: Array <String>) {
val total = sum (10, 20, 30, 40, 50)
println (total)
}``````

The sum function receives a parameter with a variable number of elements, within the algorithm we go through the array, add its elements and return the sum:

```Fun sum (vararg numbers: Int): Int {
Var sum = 0
For (element in numbers)
Sum + = element
Return sum
}
```

From the main we call to add by passing in this case 5 integers:

```    Total val = sum (10, 20, 30, 40, 50)
```

Recall that Kotlin has as the principle to allow to implement very concise algorithms, the add function we can encode it in a more concise way with the syntax:

```Fun sum (vararg numbers: Int) = numbers.sum ()
```

We use a unique expression function that calculates the sum of the array by calling the sum method of the array.

In the programming environment IntelliJ IDEA we press the “Control” key, we have the mouse arrow on a method

After pressing the mouse button will open another tab where the sum method is implemented:

This allows us to navigate in our program very quickly and to be able to analyze algorithms that are defined in the standard library of Kotlin.

Only one parameter of a function can be of type vargar and is usually the last one.

Problem 2

Elaborate a function that receives as the first parameter what type of operation we want to do with the following integer data that we send to you.

Project 171

``````enum class Operation {
SUM,
AVERAGE
}

fun operate (typeOperation: Operation, vararg array: Int): Int {
when (typeOperation) {
Operation.SUM -> return array.sum ()
Operation.AVERAGE -> return array.average (). toInt ()
}
}

fun main (args: Array <String>) {
val result1 = operate (Operation.SUM, 10, 20, 30)
println ("The sum is \$result1")
val result2 = operate (Operation.AVERAGE, 10, 20, 30)
println ("The average is \$result2")
}``````

We declare an enum class with two possible values:

```Enum class Operation {
SUM,
AVERAGE
}
```

To the operation function we send as first parameter a data type Operation and as the second parameter will be the list of integers to process:

```Fun operate (typeOperation: Operation, vararg array: Int): Int {
When (typeOperation) {
Operacion.SUM -> return arregl.sum ()
Operation.AVERAGE-> return array.average (). ToInt ()
}
}
```

When we call from the main to the function operate we must pass as the first data the type of operation that we want to do with the list of integers to send:

```Fun main (args: Array <String>) {
Val result1 = operate (Operation.SUM, 10, 20, 30)
Println ("The sum is \$result1")
Val result2 = operate (Operation.AVERAGE, 10, 20, 30)
Println ("The average is \$result2")
}
```

The convenience of the vararg parameter is the last is that if we do not respect this we will be forced to use the call to the function with named arguments. The previous program setting the vararg first will be:

```Enum class Operation {
SUM,
AVERAGE
}

Fun operate (vararg Array: Int, TypeOperation: Operation): Int {
When (typeOperation) {
Operacion.SUM -> return array.sum ()
Operation.AVERAGE -> return array.average (). ToInt ()
}
}

Fun main (args: Array <String>) {
Val result1 = operate (10, 20, 30, typeOperation = Operation.SUM)
Println ("The sum is \$result1")
Val result2 = operate (10, 20, 30, typeOperation = Operation.AVERAGE)
Println ("The average is \$result2")
}
```

We must obligatorily name the second parameter in the call:

```    Val result1 = operate (10, 20, 30, typeOperation = Operation.SUM)
```

### Spread operator

If a function receives a parameter of type vararg and from where we call it we want to send an array we must tell the compiler such a situation, With the first example we saw the code would be:

``````fun print (vararg names: String) {
for (element in names)
print ("\$element")
println()
}

fun main (args: Array <String>) {
val people = arrayOf ("Juan", "Ana", "Luis")
print (* people)
}``````

In other words, we precede the character * prior to the arrangement we send to the function.

Proposed problem

• Make a function that receives a number of ages and returns the quantity that are greater than or equal to 18 (at least one integer is sent to the function)
`Project 172`
``````fun quantityManagers (vararg ages: Int) = ages.count {it >= 18}

fun main (args: Array <String>) = println (quantityManagers (3, 65, 32, 23, 2, 98, 23, 45, 15))``````

How useful was this Lesson?

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

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?