Loading workspace environment...
Package reusable blocks of code into named functions with inputs (parameters) and outputs (return).
function name(param1, param2) { ... return result; }Declares a reusable function.const answer = name(val1, val2);Calls the function and captures its returned output.// 1. Define the reusable function
function calculateRectangleArea(width, height) {
const area = width * height;
return area; // Sends value back to caller
}
// 2. Call it for different rooms
const livingRoom = calculateRectangleArea(5, 6);
const bedroom = calculateRectangleArea(4, 3.5);
console.log("Living Room Area:", livingRoom, "sq meters");
console.log("Bedroom Area:", bedroom, "sq meters");
console.log("Combined Total:", livingRoom + bedroom, "sq meters");Living Room Area: 30 sq meters
Bedroom Area: 14 sq meters
Combined Total: 44 sq metersfunction calculateRectangleArea(width, height)Defines function with two parameter placeholders.return area;Hands the calculated result back to the caller.function add(a, b) {
console.log(a + b); // Does not return anything!
}
const result = add(2, 3); // result is undefined!function add(a, b) {
return a + b; // Returns the number
}
const result = add(2, 3); // result is 5Architectural Explanation: If a function does not have a return statement, it evaluates to undefined when assigned to a variable.
function double(n) {
return n * 2;
}
console.log(double(7));Write a function convertToFahrenheit(celsius) that returns (celsius * 9/5) + 32. Test it with 0 and 100 degrees.