Created
July 18, 2019 17:52
-
-
Save parzibyte/7c7fb0bf2367838987f04845aa10c0bb to your computer and use it in GitHub Desktop.
Variadic function in Kotlin with vararg https://parzibyte.me/blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definir función con varios argumentos | |
fun sumar(vararg numeros: Int): Int { | |
var suma = 0 | |
for (numero in numeros){ | |
suma += numero | |
} | |
return suma | |
} | |
// Invocar función variádica | |
val resultado = sumar(20, 1) | |
println("La suma es: $resultado") | |
val otroResultado = sumar(28, 11, 96) | |
println("La suma es: $otroResultado") | |
val unResultadoMas = sumar(1, 2, 3, 4, 5, 6, 7, 8, 9) | |
println("La suma es: $unResultadoMas") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment