Today, functions. A function gives a name to a piece of work so you can call it whenever you need it.
function greet() {
return 'Hello!';
}
const message = greet();
console.log(message);
Adding the parentheses to greet() is what runs the body?
Exactly. Defining it does nothing on its own. Calling it runs the body, and the value after return comes back to wherever you called it from.
It feels a little like naming a value.
Good instinct. const names a value; function names some work. The difference is that the work runs again every time you call it.
You can also write functions that accept values from the caller.
function add(a, b) {
return a + b;
}
console.log(add(1, 2));
console.log(add(10, 20));
3, then 30. The same work, different values.
a and b are the parameters; the values you pass at the call are the arguments. Think of arguments as the input and the return value as the output, and the flow is easy to follow.
But this is TypeScript, and my editor is flagging a and b.
Nice catch. It says Parameter 'a' implicitly has an 'any' type. Nothing in the code says what kind of value will arrive there.
Until now I never had to write anything.
Until now there was always a value on the right, like const price = 800;, and TypeScript could work it out from that. At the entrance of a function, no value has arrived yet.
We will fix that shortly. For now, hold on to the shape: a function takes input, does work, and returns a result. Next, handling several values at once.

