Aral Code

Functions Lesson 16 of 30

JavaScript Functions

Write reusable blocks of code with functions — parameters, arguments, return values and default values — with a grade-average and peso-change calculator.

A function is a named set of instructions you can run whenever you need it. Instead of writing the same code again and again, you write it once inside a function and call it by name. You've already used functions like alert() and Math.round(); now you'll make your own.

Creating and calling a function

Example A function with no inputs
<pre id="out"></pre>
<script>
  function sayGreeting() {
    document.getElementById("out").textContent += "Magandang umaga po! ☀️\n";
  }

  sayGreeting();   // call it once
  sayGreeting();   // and again
</script>
  • function is the keyword that creates it.
  • sayGreeting is its name (camelCase, usually a verb).
  • The code inside { } runs only when the function is called with sayGreeting().

Parameters: giving the function inputs

Functions become much more useful when they accept information. The names inside the parentheses are parameters, and the values you pass in are arguments.

Example A function with parameters
<pre id="out"></pre>
<script>
  function greet(name, section) {
    document.getElementById("out").textContent += `Hi ${name} of ${section}!\n`;
  }

  greet("Ana", "Rizal");
  greet("Ben", "Bonifacio");
  greet("Carla", "Mabini");
</script>

return: getting a value back

Most functions compute something and hand back the answer with return. You can store the returned value or use it right away.

Example Average of three grades
<pre id="out"></pre>
<script>
  function average(a, b, c) {
    return (a + b + c) / 3;
  }

  const juan = average(88, 91, 85);
  const maria = average(94, 89, 96);
  document.getElementById("out").textContent =
    "Juan: " + juan.toFixed(2) + "\nMaria: " + maria.toFixed(2);
</script>

return also ends the function immediately. Any code after a return in the same block never runs.

A sukli (change) calculator

Example Change calculator
<label>Total: ₱<input id="total" type="number" value="137"></label><br>
<label>Bayad: ₱<input id="paid" type="number" value="200"></label><br>
<button onclick="show()">Compute sukli</button>
<p id="out"></p>
<script>
  function computeChange(total, paid) {
    if (paid < total) {
      return null;   // not enough money
    }
    return paid - total;
  }

  function show() {
    const total = Number(document.getElementById("total").value);
    const paid = Number(document.getElementById("paid").value);
    const change = computeChange(total, paid);
    document.getElementById("out").textContent =
      change === null ? "Kulang ang bayad!" : `Sukli: ₱${change.toFixed(2)}`;
  }
</script>

Notice how the work is split: computeChange only does math, and show handles the page. Small functions that each do one job are easier to test and reuse.

Default parameter values

If an argument is left out, its parameter is undefined. You can give a default instead:

function greet(name = "Guest") {
  return `Welcome, ${name}!`;
}
greet("Liza");   // "Welcome, Liza!"
greet();         // "Welcome, Guest!"

Function expressions

A function can also be stored in a variable. This is called a function expression:

const square = function (n) {
  return n * n;
};
square(9);   // 81

One difference: normal function declarations can be called before the line where they're written (JavaScript "hoists" them to the top). Function expressions can only be used after they're defined.

Why use functions?

  • Reuse: write once, use many times.
  • Organize: break a big problem into small named steps.
  • Fix once: if there's a bug in the formula, you fix it in one place.
  • Readability: computeChange(total, paid) explains itself.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. In function add(a, b), what are a and b?
2. What does return do?
3. Given function f(x = 5) { return x * 2; }, what is f()?
4. What's wrong with showResult; if you want to run the function?

By Sir Reyel · Last updated 2026-12-04