#
Understanding Functions
A function is lines of code that you call from other places in your code to execute certain things. They're used far and wide, mostly when there's code you want to execute many times.
A function takes the following shape, basically:
function myFunc(arguments) {
// function code
}
A function may return something, or not. Here's an example of a function that returns a value:
function add(a, b) {
return a + b;
}
And here's one that does not return a value, but still executes something:
function log(type, message) {
console.log(`[${type}] ${message}`);
}
#
Using and Calling Functions
So the 3 previous examples were defining functions, meaning they created functions that can be used later on. Let's see how we can use them in code.
const answer = add(2, 2);
log("answer", answer); // [answer] 4
Is it that simple? Yes, it really is. Let's break down a few things before we continue.
function functionName(argument, otherargument) {
//function code
return "Some Value";
}
function
is the keyword that defines a function. There are other ways to define them, but this is the "classical" way.functionName
is the name of the function itself, how it will be called. sofunction add()
will be called asadd()
argument
andotherargument
are, of course, called the "Arguments" for the function. An Argument's name must not be confused with the Parameters that are sent to the function. That is to say, in theadd()
function,a
andb
are the arguments and2
and2
are the parameters sent to it.return
is a special keyword that does two very distinct things: first, it returns a value that's after it. So,return 2;
will return the integer2
to whatever called that function. Second, it stops the execution of the function. This means that anything after a return keyword will not execute. This is useful if you want to stop execution based on a condition. For example:
// Stop execution if either arguments are missing:
function add(a, b) {
if(!a || !b) return false;
return a + b;
};
The above will return false
if either the a
or b
parameter is missing. The a+b
line will simply never even be executed, which can save a lot of processing power in larger code.
#
Optional Arguments
In the previous examples, a
and b
were required arguments, meaning that you could not call the add function without both arguments being present (otherwise, they would be null
and our calculation would fail). You can, however, give a function optional arguments with a default value assigned.
Let's say we want add()
to increment by 1 only, if the second value is not present, instead of stopping the function. This is done by assigning a default value to b
when we create the function:
function add(a, b = 1) {
// We're still stopping the function if a is missing)
if(!a) return false;
return a + b;
}
console.log(add(5)); // 6