Foundations
Functions
Functions name reusable work and return values to the caller.
Functions
functions.js
function square(value) {
return value * value;
}
const side = 5;
const area = square(side);
console.log("side=" + side);
console.log("area=" + area);
Follow the Function Call
sidestarts at5.square(side)sends5into the function.- The function returns
5 * 5. areabecomes25.- The program prints
side=5andarea=25. | side | square result | | --- | --- | | 3 | 9 | | 5 | 25 | | 8 | 64 |
function call
A function call runs the named function and can use the returned value in another expression.
Exercise: functions.js
Reproduce area=25 for side 5, then try side 3 and 8 and predict each area before running it.