← Back to Course

⚙️ Functions and Reusable Code

Functions group related steps into one reusable unit. This makes code easier to test, reuse, and understand.

Parameters and Return Values

Example: Reusable Function

function greetUser(name: string): string {
  return `Hello, ${name}!`;
}

const message = greetUser("Ava");
console.log(message);

Why Functions Help

  • They reduce repeated code
  • They make large programs easier to organize
  • They turn complex steps into named building blocks

Example: Break a Task into Steps

function calculateTax(amount: number): number {
  return amount * 0.08;
}

function calculateTotal(amount: number): number {
  return amount + calculateTax(amount);
}

console.log(calculateTotal(50));
← Back to Code Foundations