Functions are blocks of code that you save to be (re)used later; that specific fragment of code has a predefined purpose and it does a defined task. A function could receive some or no parameters to be able to execute, and must always return something.
For example: a function that calculates the area of a square would be something like this:
function calculateArea(length, width) {
return length * width;
}If you want to use that function to calculate the area of a square with
length = 6
width = 3You need to do something like this:
let squareArea = calculateArea(6, 3);The value of squareArea will be set to 18.
- Create a new variable named
squareAreafor each new iteration of thecalculateAreafunction using the dimensions of the following figures:
For the first figure:
let squareArea1 = calculateArea(4, 4);For the second figure:
let squareArea2 = functionCall(x, y);-
Call the
calculateAreafunction 3 times, one per each square, passing the length and width of each square. -
Click here to learn more about functions: http://www.mathopenref.com/functionsprog.html
