Understand Functions

Functions

Functions allow you to reuse a piece of code.

Function Syntax

A function has a function keyword followed by zero to many arguments and a code block that runs when called. The function is typically set to a variable just like data would be.

Calling a function is how programmers say using a function. The syntax to call a function is the functions name followed by parentheses with relevant arguments within the parentheses.

// An example function
let functionName = function (argumentNameOne, argumentNameTwo, argumentNameThree) {
return (argumentNameOne * argumentNameTwo) + argumentNameThree;
}
// Printing out a function call
console.log(functionName(2, 5, 17)); // prints 27
// We build a function to be able to re-use a piece of code
console.log(functionName(3, 4, 7)); // prints 19