Funciones
Ponerle nombre a un grupo de instrucciones para poder usarlo cuantas veces quieras.
Functions
Giving a name to a group of instructions so you can use it as often as you like.
Una función es un conjunto de instrucciones al que le pones nombre. La escribes una vez y la llamas cuando la necesites.
Ya usaste una: console.log() es una función que alguien escribió por ti.
Fíjate en dos momentos distintos: definirla (escribir la receta) y llamarla (cocinarla). Definirla sola no hace nada.
A function is a set of instructions with a name. You write it once and call it whenever you need it.
You already used one: console.log() is a function someone else wrote for you.
Note two distinct moments: defining it (writing the recipe) and calling it (cooking it). Defining alone does nothing.
Una función que siempre hace exactamente lo mismo sirve poco. Lo potente es pasarle información para que se adapte.
Esos datos van entre los paréntesis y se llaman parámetros. Dentro de la función funcionan como variables normales.
A function that always does exactly the same thing is not very useful. The power is in passing it information so it adapts.
That data goes between the brackets and is called parameters. Inside the function they work like normal variables.
Una función puede recibir varios parámetros, separados por comas:
A function can take several parameters, separated by commas:
Hasta ahora las funciones mostraban cosas. Pero lo más útil es que calculen algo y te lo devuelvan para que tú decidas qué hacer con el resultado.
Eso se hace con return.
So far the functions displayed things. But the most useful thing is for them to work something out and hand it back so you decide what to do with the result.
That is what return is for.
Un detalle importante: return termina la
función ahí mismo. Lo que venga después no se ejecuta nunca.
One important detail: return ends the function
right there. Anything after it never runs.
Tres razones muy concretas:
- No te repites. Si algo se hace en cinco lugares y hay que corregirlo, lo arreglas en un solo sitio.
- Se entiende mejor. Leer
calcularPuntaje()dice más que veinte líneas de cuentas. - Se prueba por partes. Si algo falla, sabes en cuál función mirar.
Compara estas dos formas de hacer lo mismo:
Three very concrete reasons:
- You do not repeat yourself. If something happens in five places and needs fixing, you fix it in one.
- It reads better. Seeing
calculateScore()says more than twenty lines of arithmetic. - It is testable in pieces. When something fails, you know which function to look at.
Compare these two ways of doing the same thing:
Variables para recordar. Decisiones para elegir. Bucles para repetir. Funciones para organizar.
Con esas cuatro ideas está construido absolutamente todo el software que has usado en tu vida. En serio: lo demás son variantes, herramientas y mucha práctica.
Ahora viene la parte entretenida: ponerlo en pantalla.
Variables to remember. Decisions to choose. Loops to repeat. Functions to organise.
Every piece of software you have ever used is built from those four ideas. Genuinely: the rest is variations, tooling and a lot of practice.
Now for the fun part: putting it on screen.