← Fundamentals
Fundamentals

Arguments, values, and references

What does the callee receive, and which of its changes can the caller see?

A parameter’s value and access to other storage determine which changes can reach its caller.

Outcome

Predict whether a callee’s change reaches the caller from the parameter’s type and passing mode, and choose between sharing, copying and borrowing deliberately.

Terminology (6)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.
Slice header
Go’s pointer, length and capacity triple; copying it shares the backing array.
Pointer parameter
A *T parameter, through which the callee changes the caller’s T.

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 takes a Cart; applyCouponTo takes a *Cart and is called with &cart.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

type Cart struct {
	Total int
}

func applyCoupon(cart Cart) {
	cart.Total -= 5
}

func applyCouponTo(cart *Cart) {
	cart.Total -= 5
}

func main() {
	cart := Cart{Total: 40}

	applyCoupon(cart)

	fmt.Println(cart.Total)

	applyCouponTo(&cart)

	fmt.Println(cart.Total)
}
What do the two lines print?
Go · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
go run .

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 []int parameter with sort.Ints.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import (
	"fmt"
	"sort"
)

func median(prices []int) int {
	sort.Ints(prices)

	return prices[len(prices)/2]
}

func main() {
	prices := []int{30, 10, 20}

	fmt.Println(median(prices))
	fmt.Println(prices)
}
What does the second line print?

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 base has explicit length 2 and capacity 4. After one call, the caller prints its length and ordinary view, then a three-element view within that capacity.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import "fmt"

func withStop(route []string, stop string) {
	route = append(route, stop)
	fmt.Println("callee", len(route))
}

func main() {
	base := make([]string, 2, 4)
	base[0], base[1] = "depot", "market"

	withStop(base, "north")
	fmt.Println("caller", len(base), base)
	fmt.Println("view", base[:3])
}
What do the callee, caller and extended view lines print?
Loading Two routes from one base…

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.

  1. easy · F-PASS-V4-1 Apply coupons to the cart
  2. medium · F-PASS-V4-2 Find the median, keep the order
  3. hard · F-PASS-V4-3 Branch routes from one base
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.

Example.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
'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

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.