Function Definition
Basic Declaration
Section titled “Basic Declaration”texto saludar(texto nombre) { retornar "Hola " + nombre}- Functions require an explicit return type (
vaciowhen they don’t return a value). - The body is defined between braces.
retornarends execution and returns the indicated value.
Functions Without Return
Section titled “Functions Without Return”vacio mostrar_mensaje(texto mensaje) { consola.mostrar(mensaje)}Functions Mutating Parameters
Section titled “Functions Mutating Parameters”Parameters are immutable by default. To allow modification, use the var keyword within the signature.
texto agregar_sufijo(texto var base) { base += " agregado" retornar base}Recursive Functions
Section titled “Recursive Functions”entero factorial(entero n) { si (n == 0) { retornar 1 } retornar n * factorial(n - 1)}Returning Objects
Section titled “Returning Objects”Functions can return object instances or lists.
// Function that returns a numbernumero sumar(numero a, numero b) { retornar a + b}
// Function that returns an integerentero multiplicar(entero a, entero b) { retornar a * b}
// Function that returns texttexto concatenar(texto a, texto b) { retornar a + b}
// Function that returns a booleanlog es_par(entero valor) { retornar valor % 2 == 0}
// Function that returns JSONjsn obtener_datos() { retornar { nombre: "Quetzal", version: "0.0.2", tipos_soportados: ["entero", "numero", "texto", "log", "lista", "jsn"] }}
// Function that returns a typed integer listlista<entero> obtener_numeros() { retornar [1, 2, 3, 4, 5]}
// Function that returns an untyped list of different typeslista obtener_datos_lista_no_tipada() { retornar [1, "texto", verdadero, 3.14, [1, 2, 3]]}
// Define an objectobjeto DefinicionUsuario { privado: texto nombre entero edad publico: DefinicionUsuario(texto nombre, entero edad) { ambiente.nombre = nombre ambiente.edad = edad }
texto obtener_nombre() { retornar ambiente.nombre }
entero obtener_edad() { retornar ambiente.edad }}
// Function that returns an object of type DefinicionUsuarioDefinicionUsuario crear_usuario(texto nombre, entero edad) { retornar nuevo DefinicionUsuario(nombre, edad)}Overloading and Names
Section titled “Overloading and Names”Currently Quetzal does not support function overloading; each name must be unique in the same scope. You can group related behaviors in modules or objects.
Best Practices
Section titled “Best Practices”- Use descriptive names in Spanish following
camelCaseorsnake_case. - Validate parameters at the start of the function and use
lanzarto notify errors. - Keep functions short; extract logic into helper functions when necessary.