Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/marioaje/Python/llms.txt

Use this file to discover all available pages before exploring further.

Functions are one of the most important building blocks in any programming language. In Python, a function is a named block of code that you can write once and run as many times as you need. As the course comments put it, functions help simplificar el desarrollo, reutilizar codigo — they simplify development and make your code reusable. Instead of writing the same logic in multiple places, you wrap it in a function, give it a name, and call it wherever it is needed.
Every Python function is introduced with the def keyword, followed by the function name, parentheses (which may contain parameters), and a colon. The body of the function must be indented — Python uses indentation to define the scope of the function.

Section 1 — Function with No Parameters

The simplest kind of function takes no input at all. It simply runs the same code every time it is called.
funciones.py
# funcion sin parametros o argumentos
def imprimeMensaje():
    print("Mensaje de bienvenida")
imprimeMensaje() prints a fixed welcome message. Because it has no parameters, the parentheses in both the definition and the call are empty. This is useful when a piece of logic is always identical — no external data is required.

Section 2 — Functions with Parameters

Parameters let you pass information into a function so it can work with dynamic data rather than hard-coded values.

Single parameter

funciones.py
# funcion de mensaje con parametro
def imprimeMensajePersonalizado(textoMensaje):
    print(textoMensaje)
Here textoMensaje is a parameter — a local variable that receives whatever value you pass when calling the function. You can pass any string, number, or object.

Multiple parameters

funciones.py
# funcion de mensaje con parametro
def imprimeMensajePersonalizados(textoMensaje, test):
    print(textoMensaje)
Adding a second parameter (test) shows how Python separates multiple parameters with commas. Both values are available inside the function body, though in this example only textoMensaje is printed.

A practical two-parameter example

The suma function from Week 1 is a great real-world illustration of a function that receives two parameters and performs a computation:
Semana 1/suma.py
def suma(a, b):
      print(a + b)

operacion = 0
# Ingreso de datos
dato1 = input("Ingresa el valor para dato1:")
dato2 = input("Ingresa el valor para dato2:")

# convierte los datos al tipo de dato a convenir int(),
# me permite convertir un texto a un numero
dato1 = int(dato1)
dato2 = int(dato2)

# operacion matematica
operacion = dato1 + dato2

# impresion del resultado
suma(dato1, dato2)
Notice that input() always returns a string, so the values are converted with int() before being passed to suma. This is a common pattern whenever you collect numeric input from a user.

Section 3 — Functions with a Return Value

So far every function has used print() to show output. But often you need a function to produce a value that your program can store and use later. That is what return is for.
funciones.py
# funcion que retorna datos
def funcionRetorno():
    return 3 + 3

def cualquiera():
    return 6 + 6
funcionRetorno() evaluates 3 + 3 and hands the result (6) back to whoever called it. cualquiera() does the same with 6 + 6, returning 12. The calling code receives those values and can store them in variables or use them in expressions.
print() vs return — they look similar but behave very differently.
  • print() displays a value on the screen. Once the function finishes, that value is gone; the caller receives None.
  • return hands a value back to the caller. The caller can store it, pass it to another function, or use it in a calculation.
Prefer return whenever the result of a function needs to be used elsewhere in your program. Use print() only when the sole purpose of the function is to show something to the user.

Section 4 — Calling Functions

Defining a function does nothing on its own — you must call it to run its code. The source file includes commented-out examples that demonstrate the three calling styles:
funciones.py
# imprimeMensaje()
# variable = "Hola clase"
# imprimeMensajePersonalizado(variable)

variable = funcionRetorno()
print(variable)
1

Call a no-parameter function

Simply write the function name followed by empty parentheses:
imprimeMensaje()
Python finds the function, runs its body, and returns to the next line.
2

Call a function with a parameter

Pass the argument inside the parentheses — either a literal or a variable:
variable = "Hola clase"
imprimeMensajePersonalizado(variable)
The string "Hola clase" is assigned to textoMensaje inside the function.
3

Capture a return value

Assign the function call to a variable so you can use the result:
variable = funcionRetorno()
print(variable)   # prints 6
Without the assignment the returned value would be discarded.

No parameters

imprimeMensaje() — just call the name with empty parentheses.

With parameters

imprimeMensajePersonalizado(variable) — pass data inside the parentheses.

Two parameters

imprimeMensajePersonalizados(texto, test) — separate multiple arguments with commas.

Return value

resultado = funcionRetorno() — capture the returned value in a variable.

Build docs developers (and LLMs) love