Function Parameters, Arguments and Return Values

Agree on what a calculation receives and produces

A report asks a total function to combine two marks. Both caller and function need a shared understanding of the input order and the result; otherwise valid code can calculate the wrong quantity.

A parameter is a name in a function definition. An argument is a value supplied by a call. return sends a value to the caller and ends that function call.

  1. Match inputs

    Map arguments to parameters in order.

  2. Use results

    Store, combine or display a returned value.

  3. Trace exits

    Explain early returns, missing arguments and default values.

Bind two arguments, then return a value

  1. Arguments

    Values are supplied at the call site.

    calculateTotal(40, 35)
  2. Parameters

    Local names receive those values.

    maths = 40; science = 35
  3. Return

    The caller stores the result.

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

The caller’s names do not need to match the parameters

A call passes values, not a requirement that the caller use the same variable names. Positional arguments bind to parameters in order, so swapping arguments can matter for operations such as subtraction.

A returned expression is evaluated before the function exits. The caller can use the result in another expression. Statements after an executed return are not reached during that call.

Before starting, review defining and calling functions. These examples use JavaScript; other languages can use different syntax and rules.

Store the returned total before displaying it

The call supplies 40 and 35. Addition produces 75, return ends the call, and that value initializes total.

The function does not print anything itself. The final two lines use the result for output and an average.

JavaScript · local teaching example

function calculateTotal(maths, science) {
  return maths + science;
}
const firstMark = 40;
const secondMark = 35;
const total = calculateTotal(firstMark, secondMark);
console.log(total);
console.log(total / 2);

Expected output

75
37.5

A default is a deliberate input policy

JavaScript uses a default parameter when an argument is omitted or supplied as undefined. It does not use that default merely because the argument is zero.

This allowance function adds a default bonus of five, while an explicit zero bonus remains zero. Defaults should encode a real requirement, not conceal missing required marks.

JavaScript · local teaching example

function correctedMark(mark, bonus = 5) {
  return mark + bonus;
}
console.log(correctedMark(35));
console.log(correctedMark(35, 0));

Expected output

40
35

Passing a record can expose shared changes

JavaScript passes the object reference as a value. Changing the parameter’s field changes the shared object the caller also uses; this is not a deep copy.

Reassigning the local parameter to a different object would not reassign the caller’s variable. Keep a function’s mutation policy explicit.

JavaScript · local teaching example

function correct(student) {
  student.science += 5;
}
const student = { science: 35 };
correct(student);
console.log(student.science);

Expected output

40

Apply the rule to a small change

Write a function that returns maths minus science, then call it with 40 and 35 and with the arguments reversed.

Worked answer

Argument order matters: 40 - 35 gives 5, while 35 - 40 gives -5. Name parameters to make the contract clear.

JavaScript · local teaching example

function markDifference(maths, science) {
  return maths - science;
}
console.log(markDifference(40, 35));
console.log(markDifference(35, 40));

Expected output

5
-5

Recognize mistakes and limits

Omitted JavaScript arguments normally leave a parameter undefined unless a default applies. Adding undefined to a number can yield NaN. Decide which arguments are required and reject unsuitable inputs.

A function returning an object still returns one value; that value can group several fields. Other languages may support multiple-result syntax, named arguments or different parameter rules. Do not generalize JavaScript positional behavior to every language.

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;
}
const firstMark = 40;
const secondMark = 35;
const total = calculateTotal(firstMark, secondMark);
console.log(total);
console.log(total / 2);
Which term names the values 40 and 35 supplied by the call?
Choose one answer. You can try again.

Answer and explanation

Arguments.

Yes. Arguments are the values supplied by a call.

Apply the idea to a new situation

JavaScript · code for this question

function correctedMark(mark, bonus = 5) {
  return mark + bonus;
}
console.log(correctedMark(35));
console.log(correctedMark(35, 0));
Why does correctedMark(35, 0) return 35?
Choose one answer. You can try again.

Answer and explanation

The explicit zero bonus is used rather than the default five.

Yes. Defaults apply to omitted or undefined arguments, not every falsy value.

Write a clear input and result contract

Parameters name inputs, arguments supply them, and return sends a value to the caller. Order, defaults, validation and object mutation all belong to a function’s contract.

Connect this idea with local names and scope and testing a function contract.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Variable Scope: Local, Global and Enclosing Names →