Functions: Define, Call and Reuse a Task

Give a useful school calculation a name

Several school reports need the same calculation: add maths and science marks. Repeating the calculation in every report makes a later change easy to miss.

A function groups instructions under a callable operation. A definition describes the work; a call asks it to run. A returned result lets the caller decide how to use the calculation.

  1. Read the definition

    Identify the function name, inputs and body.

  2. Follow a call

    Explain when the body runs and which result reaches the caller.

  3. Reuse a task

    Apply one calculation to different students without duplicating the rule.

A call connects inputs to a reusable calculation

  1. Caller supplies data

    Use this student’s two marks.

    40, 35
  2. Function body

    Apply the shared addition rule.

    maths + science
  3. Caller receives result

    The caller chooses to display it.

    75
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

A definition alone does not perform the school task

JavaScript function introduces a definition, followed by its name, parameters in parentheses and body in braces. Parameters give names to values supplied by a call.

Calling calculateTotal(40, 35) runs the body with those values. return sends a result back to the caller. console.log then displays that result; calculating and displaying are separate responsibilities.

Before starting, review numeric calculations. These examples use JavaScript; other languages can use different syntax and rules.

Call one calculation for two students

Each call supplies a different pair of marks. The first returns 75, and the second returns 70. The function body uses the same rule in both calls.

The function does not need a separate name for each student. The arguments provide the data that changes.

JavaScript · local teaching example

function calculateTotal(maths, science) {
  return maths + science;
}
console.log(calculateTotal(40, 35));
console.log(calculateTotal(28, 42));

Expected output

75
70

A printed message is not a returned result

This routine displays a total but has no return expression. JavaScript returns undefined when a function reaches its end without returning a value.

The caller prints undefined after the routine has printed 75. Return the total if callers need to reuse it in another calculation.

JavaScript · local teaching example

function printTotal(maths, science) {
  console.log(maths + science);
}
const result = printTotal(40, 35);
console.log(result);

Expected output

75
undefined

Apply the rule to a small change

Use a total function for marks 25 and 30, store its result, then display the average across two subjects.

Worked answer

The total function returns 55. The caller can divide that returned result by two and display 27.5 without changing the total function.

JavaScript · local teaching example

function calculateTotal(maths, science) {
  return maths + science;
}
const total = calculateTotal(25, 30);
console.log(total / 2);

Expected output

27.5

Recognize mistakes and limits

Name a function by its purpose and keep its responsibility focused. A function that totals marks, writes a file, changes permissions and sends a message is harder to reuse and test.

A function does not validate inputs automatically. Document whether it accepts only finite numeric marks and where the range check belongs. Avoid hidden dependence on global marks when explicit parameters are clearer.

For the language or tool rules used here, see JavaScript functions and scope.

Check the core idea

JavaScript · code for this question

function calculateTotal(maths, science) {
  return maths + science;
}
console.log(calculateTotal(40, 35));
console.log(calculateTotal(28, 42));
When does calculateTotal execute its body?
Choose one answer. You can try again.

Answer and explanation

When a call supplies arguments and requests the calculation.

Yes. The definition describes work; the call performs it.

Apply the idea to a new situation

JavaScript · code for this question

function printTotal(maths, science) {
  console.log(maths + science);
}
const result = printTotal(40, 35);
console.log(result);
Why did result become undefined in the printing example?
Choose one answer. You can try again.

Answer and explanation

No return expression supplied a result to the caller.

Yes. Printing is a side effect and does not replace return.

Define once, call with the data that changes

A function names reusable work. Distinguish its definition, call, returned result and any side effects. Focused calculations can support several reports and tests.

Connect this idea with parameters and returned results and shared business rules.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Function Parameters, Arguments and Return Values →