We hope you are ready - because you are on the brink of one of the most fun parts of writing JavaScript: writing functions. A function is a procedure of code that will run when called. When you write a function, you can use it as many times as you please. You only write a function once. Writing a function is known as a function definition. Functions are the fundamental building blocks of JavaScript and mastering them is a big step on the road to JavaScript mastery!
A function is a set of code that will run when called. Functions really start to make sense when put in the perspective of solving problems. For example, imagine you want to use code to find the average of two given numbers minus 5. You can write code that takes two numbers, adds them together, then divides their sum by two, and finally subtract 5 from it:
// Run the "node" command in the terminal
// Then paste the following code in the terminal
(5 + 5) / 2 - 5 // evaluates to 0
(15 + 3) / 2 - 5 // evaluates to 4
(7 + 2) / 2 - 5 // evaluates to -0.5
Writing out the same code again and again is tedious. Instead, you can declare a re-usable function.
A function definition is the position in the code where the function is created. It's where a function is saved in the code so that you can use it or re-use it.
A function declaration is just one way to define a function. (You will learn only this one way for now.) A function declaration consists of the function keyword, followed by three components:
Let's think back to the code to find the average of two numbers minus 5. Instead of manually writing out the math whenever you want to find this value for a new pair of numbers, you could write a function to handle the logic of calculating the value for any two number.
For example, you could write a function that:
function averageMinus5(num1, num2) { // function name and parameters
return (num1 + num2) / 2 - 5; // code to be run
}
First thing to notice for the above averageMinus5 function is that didn't use any real numbers. You always want to write functions to accept as wide a range of data as possible. Utilizing the incoming parameters to a function is key to keeping functions flexible and re-usable.
In the case of the averageMinus5 function, you want to use it to calculate the average of any two numbers minus 5. The parameters or the inputs for the averageMinus5 function are num1 and num2. In other words, the averageMinus5 function expects to be given two numbers, that will be referenced by the variable names, num1 and num2, in the code to be run.
You'll learn more about parameters later. For now, know that when you define a function with parameters you are declaring those parameters as usable variables within that function.
The beauty of a function is that if you define it in a clever way, it will be highly re-usable with a lot of different data! For example, your averageMinus5 function should work with any two numbers.
What if you wanted the averageMinus5 function to take the average of two input numbers minus another third input number? You can refactor or change the function's code to:
function averageMinusNum(num1, num2, num3) {
return (num1 + num2) / 2 - num3;
}
A quick but very important side note about good naming. Take this to heart right now: Good names are important. Do yourself, and every other programmer reading your code, a favor by always using significant function and variable names.
For example, x is a NOT a descriptive name for a variable or function. Other programmers will not understand what the variable or function is supposed to do or represent.
As you tackle more complicated problems and your code grows to be more complex, you are likely to forget what badly named variables originally stood for and what their purpose was. Non-descriptive names make your code error-prone. Great code reads like English and almost explains itself. A word for code that reads like English and explains itself is semantic code.
As programmers, your goal is to write code that is not only correct, but also elegant, readable, and maintainable! Hold yourself to this high standard.
As far as syntax goes in JavaScript, you always name your functions and variables camelCase for multiple words. (Ex: tipCalculator, currentNumber, puppyPartyFinder). Other languages use other conventions so it's best to pick up the standard for your chosen language and stick with it.
Next, you will learn how to invoke or use a function with input data (arguments).