Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 18, 2019 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/7c7fb0bf2367838987f04845aa10c0bb to your computer and use it in GitHub Desktop.
Save parzibyte/7c7fb0bf2367838987f04845aa10c0bb to your computer and use it in GitHub Desktop.
Variadic function in Kotlin with vararg https://parzibyte.me/blog
// 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