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.
Match inputs
Map arguments to parameters in order.
Use results
Store, combine or display a returned value.
Trace exits
Explain early returns, missing arguments and default values.
Bind two arguments, then return a value
Arguments
Values are supplied at the call site.
calculateTotal(40, 35)Parameters
Local names receive those values.
maths = 40; science = 35Return
The caller stores the result.
total = 75
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.5A 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
35Passing 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
40Apply 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
-5Recognize 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);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));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.