Terminology (5)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.
- Pass by value
- The call initializes a separate parameter value. That value can still hold a reference or pointer to shared data; Rust and C++ can also move owned values into value parameters.
- Shared data
- Data reachable from both caller and callee, so a change through either is visible to both.
- Rebinding
- Replacing a local parameter binding or its object-reference value, as in emptyCart. Assigning through a bound reference or dereference instead writes the referred-to object; it does not reseat that local binding.
- In-place operation
- An operation such as sort or push that changes the existing value rather than producing a new one.
- Object reference
- What a TypeScript object or array variable holds; copying it shares the same object.
Follow the value and the write destination
Parameter passing establishes what a callee receives: a value, a reference, or a value that itself provides access to another object. Rebinding a parameter, modifying an object reached through it and returning a new value are distinct operations. Follow the destination of each write.
A copy is meaningful only when its level is clear. Copying a container or handle may preserve access to shared elements; creating independent working storage may require another step. Use the selected parameter type and actual call to decide which data can change and which data must remain available to the caller.
01Updating a record
Does the coupon reach the retained cart?
A checkout preview keeps an order draft open while it applies coupons, reports item prices and compares delivery routes. A coupon must change the retained total; a price statistic must leave the item order alone; each proposed route must coexist with the base. What must each helper receive to meet its part of that job?
Start with the checkout total. This reduction gives a cart 40, calls a coupon helper, then makes a second call on the same cart. The caller prints its retained total after each call. Predict both observations before deciding whether the helper changed a local value or the cart that checkout still holds.
applyCoupon changes a property of its parameter; emptyCart assigns a new object to its parameter.
type Cart = {
total: number;
};
function applyCoupon(cart: Cart): void {
cart.total -= 5;
}
function emptyCart(cart: Cart): void {
cart = { total: 0 };
}
const cart: Cart = { total: 40 };
applyCoupon(cart);
console.log(cart.total);
emptyCart(cart);
console.log(cart.total);
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. Some probes intentionally attempt an operation the parameter type refuses; predict that outcome before changing the source.
02Changing a list in place
Can the price statistic leave receipt order alone?
The receipt preserves the order in which items were entered, while median needs their sorted rank. This probe attempts a median from 30, 10, 20 before printing the caller’s list. Use the selected parameter type to predict a caller observation or a refusal to perform that sorting operation.
median sorts its array parameter with sort.
function median(prices: number[]): number {
prices.sort((a, b) => a - b);
return prices[Math.floor(prices.length / 2)]!;
}
const prices = [30, 10, 20];
console.log(median(prices));
console.log(prices.join(" "));
03Growing a list
What does growing this route change in the caller?
The delivery preview needs proposed routes while keeping the canonical starting route available. First inspect growth across a call boundary. The selected probe shows which storage the helper can change and what the caller observes afterwards.
The helper pushes one stop and returns nothing. The caller then prints its base array.
function withStop(route: string[], stop: string): void {
route.push(stop);
}
const base = ["depot", "market"];
withStop(base, "north");
console.log(base.join(" "));
04Repair the checkout
Update the cart, keep the caller’s order, branch routes safely
The supplied OrderDraft reader keeps the subtotal, input-ordered prices and canonical route together. Later files run the completed checkout stages before reaching the helper you repair, so a route result still belongs to the same discounted order.
Work through the same draft’s three requirements: keep coupon changes in its total, include every item in its median while preserving receipt order, then retain delivery alternatives independently from the canonical route. A free item still occupies a line in the receipt and belongs in the price statistic.
The lab opens in your reading language and has its own language control. Each file contains its imports, types, reader and complete main.
Repair the checkout
Reaching the caller’s data, protecting the caller’s data, then telling a new list from a shared one.
In the frontend Keep checkout state in the caller Optional
A checkout component is another caller that retains a Cart. A helper can return a replacement without changing the Cart it received; the component must adopt that returned value. These excerpts make that handoff explicit.
'use client';
import { useState } from 'react';
type Cart = { total: number };
function discounted(cart: Cart, amount: number): Cart {
return { ...cart, total: cart.total - amount };
}
export default function Checkout() {
const [cart, setCart] = useState<Cart>({ total: 40 });
return (
<button onClick={() => setCart((current) => discounted(current, 5))}>
Total {cart.total}
</button>
);
}
React treats state objects as read-only. The helper builds a new Cart and the state updater adopts it. Assigning a new object only to the helper’s parameter would leave the component’s state binding unchanged.
Sources: React: updating objects in state,Svelte: deeply reactive $state.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?