Functions in Nyx are defined with fn, take typed parameters, and declare their return type after ->. They support default parameter values, making call sites cleaner when a sensible default exists.
// Funciones: parámetros, valor de retorno, default parameters fn suma(a: int, b: int) -> int { return a + b } fn saludar(nombre: String, saludo: String = "Hola") -> String { return saludo + ", " + nombre + "!" } fn describir(n: int) -> String { if n > 0 { return "positivo" } if n < 0 { return "negativo" } return "cero" } fn main() -> int { let resultado: int = suma(3, 4) print("3 + 4 = " + int_to_string(resultado)) print(saludar("Mundo")) print(saludar("Nyx", "Buenos dias")) print("42 es " + describir(42)) print("-5 es " + describir(-5)) print("0 es " + describir(0)) return 0 }
3 + 4 = 7 Hola, Mundo! Buenos dias, Nyx! 42 es positivo -5 es negativo 0 es cero
How it works
Functions are declared with the fn keyword followed by the function name, a parenthesized parameter list, and the return type. Each parameter has a name and a type separated by a colon. The function body is a block enclosed in { }.
Default parameter values are written as param: Type = value. When a caller omits that argument, the default is used automatically. In saludar, calling saludar("Mundo") uses the default greeting "Hola", while saludar("Nyx", "Buenos dias") overrides it.
The built-in int_to_string function converts an integer to its string representation. Nyx does not coerce types implicitly, so you must convert explicitly when building strings from non-string values.
Functions can have multiple return statements. The compiler requires that all paths return a value of the declared type — any branch that falls off without returning causes a compile-time error.