ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Introduction to Computer Programming• 6 min read

Functions, Parameters & Return Values

Structure logic into reusable functions, pass arguments, return calculated values, and prevent code duplication.

The Building Blocks of Modularity

A function is a self-contained block of code designed to perform a specific task. Functions take input arguments, execute algorithmic logic, and return an output.

Pure Reusable Function
function calculateTax(subtotal, taxRate = 0.08) {
  if (subtotal < 0) throw new Error("Subtotal cannot be negative");
  return Number((subtotal * (1 + taxRate)).toFixed(2));
}

console.log("Total Order:", calculateTax(150.00)); // 162.00
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

What keyword passes the final computed result from inside a function back to the calling code?