Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Argument
- A value supplied at a call site, evaluated before the call begins.
- Parameter
- For the scalar by-value calls shown, a local binding initialized from the argument in the same position.
- Return value
- The value a call produces; it replaces the call expression in the caller.
- Callback
- A function passed to another function, which decides when to call it.
The boundary of one invocation
A call evaluates arguments and binds them to the function’s parameters. In the positional calls here, a caller’s variable names do not choose the parameter positions. The body runs with those bindings, and any returned value becomes the value of that call expression.
Keep the call hierarchy visible when a helper invokes another function. A callback has its own invocation boundary, and the helper defines how it uses the callback’s result. The caller still needs to store or otherwise use the result it requires.
01Binding arguments
Arguments bind to fresh parameters by position
An invoice reconciliation tool has a due day, a paid day and an amount for each settled invoice. It must calculate the late fee and identify the first late payment for review. Before trusting the report, check how those day values enter a helper and how its result reaches the report.
The input reader supplies two day numbers with the same type. The invoice tool needs paid minus due, with early payments counted as zero days late. This probe calls daysLate in both argument orders and prints the caller’s paid value afterwards. Predict all three numbers.
function daysLate(due: number, paid: number): number {
paid = paid - due;
return Math.max(0, paid);
}
const due = 10;
const paid = 14;
console.log(daysLate(due, paid), daysLate(paid, due), paid);
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment. Days are whole-number indexes on one timeline, not calendar dates. The supplied small amounts and day differences stay exactly representable and within the integer types shown.
02Using the result
Keep the amount a helper computes
The tool charges 2 billing units per day late. addFee takes an amount and a day count and returns the amount including that fee. This probe calls it once as a statement and once in an assignment. Predict both printed totals.
function addFee(amount: number, days: number): number {
return amount + days * 2;
}
let total = 100;
addFee(total, 3);
console.log(total);
total = addFee(total, 3);
console.log(total);
03Which function returns
return leaves the innermost function
The review list must choose its first qualifying invoice in input order. Isolate that selection question with firstLarge, which searches a short list of day counts for the first value above 10. It records matches inside an iteration callback. Predict the printed value.
Array.prototype.forEach calls the arrow function once for each element.
function firstLarge(sizes: number[]): number {
let found = -1;
sizes.forEach((size) => {
if (size > 10) {
found = size;
return;
}
});
return found;
}
console.log(firstLarge([4, 12, 20, 7]));
04Repair the invoice tool
Use the total, stop at the first overdue invoice, charge the right fee
Finish three parts of the reconciliation report: an amount including its late fee, the first invoice paid late in issue order, and a fee that agrees with the day count printed beside it. The supplied readers and callers keep the work on call boundaries.
The lab opens in your reading language and has its own language control. Each file contains its imports, types, reader and complete main. Later levels supply the earlier correct helpers.
Repair the invoice tool
Keep a computed total, complete an ordered search, then carry a pricing callback’s result through the fee report while preserving the shared day-count helper.
In the frontend Pass a function to the button, then bind the invoice ID when it runs. Optional
A client-side invoice review list receives a review callback from its client parent. The button must supply the invoice ID when its handler runs. Constructing the button should only pass a function; it should not call the review action.
"use client";
type Props = {
invoiceId: string;
onReview: (id: string) => void;
};
export default function InvoiceAction({ invoiceId, onReview }: Props) {
function review() {
onReview(invoiceId);
}
return <button onClick={review}>Review invoice</button>;
}
onClick receives the review function. When React calls that handler, its body calls onReview with the invoiceId argument. Writing onClick={onReview(invoiceId)} would call the supplied action during render and pass its void result as the handler. This callback comes from a client parent; it is not an arbitrary function serialized across a Server Component boundary.
Sources: React: responding to events,Next.js: use client boundary and serializable props,Svelte: event attributes.
Sign in to keep your read marks on every device. Sign in
Was this helpful?