Procedures & functions · Procedimientos y funciones
A function is a named block
- A function is a named block of code you can run again and again.
- Define it once with
def, then call it by name. - This avoids repeating yourself and keeps programs tidy.
Una función es un bloque con nombre
- Una función es un bloque de código con nombre que puedes ejecutar una y otra vez.
- Defínela una sola vez con
def, luego llámala por su nombre. - Esto evita la repetición y mantiene los programas ordenados.
def greet():
print("Hello!")
greet()
greet()
Parameters pass data in
- A parameter is a value the function receives.
- Put it in the brackets, and pass an argument when you call.
- Keep the number of parameters small so the function stays easy to read.
Los parámetros reciben datos
- Un parámetro es el valor que recibe la función.
- Colócalo entre corchetes y pasa un argumento al llamarla.
- Mantén el número de parámetros bajo para que la función siga siendo fácil de leer.
def greet(name):
print("Hello, " + name)
greet("Ada")
greet("Sam")
Functions return a value
- A function hands a value back with
return. - A block that only does something and returns nothing is a procedure.
Las funciones devuelven un valor
- Una función devuelve un valor mediante
return. - Un bloque que solo realiza una acción y no devuelve nada se denomina procedimiento.
def square(n):
return n * n
answer = square(6)
print(answer) # 36
Call and return · Llamada y retorno
Calling runs the function's body; return hands a value back. · La llamada ejecuta el cuerpo de la función; return devuelve un valor.
In the exam: PROCEDURE and FUNCTION
- Pseudocode separates a
PROCEDURE(no result) from aFUNCTION(returns a value).
En el examen: PROCEDIMIENTO y FUNCIÓN
- El pseudocódigo separa un
PROCEDURE(sin resultado) de unaFUNCTION(que devuelve un valor).
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
Common mistakes
- A function returns a value with
return; a procedure just does a job. - Call it with
(). - Parameters receive the arguments in order.
Errores comunes
- Una función devuelve un valor con
return; un procedimiento simplemente ejecuta una tarea. - Llámala con
(). - Los parámetros reciben los argumentos en orden.
Now you try
- Write each function — it is then called for you in the check.
- Press Check answer to test your code.
Ahora tú lo intentas
- Escribe cada función; luego será llamada automáticamente en la verificación.
- Pulsa Check answer para probar tu código.
Write a function · función double(n) that returns · retornos n times 2. · Escribe una función double(n) que devuelva n multiplicado por 2.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write a function is_even(n) that returns True when n is even, otherwise False. · Escribe una función is_even(n) que devuelva True cuando n sea par, de lo contrario False.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write a procedure print_stars(n) that prints a line of n star (*) characters, then call it with 4 so it prints ****. · Escribe un procedimiento print_stars(n) que imprima una línea de n caracteres estrella (*), luego llámalo con 4 para que imprima ****.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.