← Fundamentals
Fundamentals

Changing a caller’s value through a pointer

How can a function update a variable it does not own?

Identify how a helper’s parameter reaches the caller-owned value it must update.

Outcome

Tell a value parameter from a pointer, &mut or reference parameter, write through the parameter instead of a local copy or a re-pointed parameter, and recognize the copies a method receiver or a loop variable makes.

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.

Out parameter
A parameter a function writes through to give the caller a result.
Pass by value
Giving a function a copy of the argument.
Mutable borrow
A Rust &mut reference that lets a function change a value it does not own.
Reference parameter
A C++ parameter declared with & that is another name for the caller’s variable.
Receiver
The argument supplied to a Go method: a value receiver receives a copy; a pointer receiver receives a pointer to the selected target, which may itself be a local copy.

A parameter and its write target

Go passes arguments by value, including pointer values. A *T parameter receives an address through which the function can access a caller-owned T. Its local pointer and the value reached through it remain distinct assignment targets.

Trace every copy made inside the helper as well as the original call. Having a pointer parameter grants a route to the caller’s value; it does not make every local calculation write back through that route.

01A copy

A value parameter is the function’s own copy

A garden-plan editor imports planned bed quantities and harvested counts into caller-owned records. It can add to one bed, append to a harvest record, or apply a bulk addition followed by transfers. The imported changes must reach the retained planning file through each native pointer, borrow or reference.

A planning import calls addPlants with the stored bed count. This first version receives an integer by value and adds 4. Predict its local output and the count printed by the caller.

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

import "fmt"

func addPlants(bed int, extra int) {
	bed += extra

	fmt.Println("inside", bed)
}

func main() {
	bed := 12
	addPlants(bed, 4)

	fmt.Println(bed)
}
What is printed?
Go · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
go run main.go

Use the selected native setup from the first reading. For each probe, consider whether the source is accepted and which caller-owned value is still reachable when the function returns.

02Through the parameter

A pointer, &mut or reference parameter leads back to the caller’s variable

The import must retain the added seedlings after the helper returns. This version supplies a pointer, mutable borrow or reference to the count; trace the write and predict the caller’s output.

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

import "fmt"

func addPlants(bed *int, extra int) {
	*bed += extra
}

func main() {
	bed := 12
	addPlants(&bed, 4)

	fmt.Println(bed)
}
What is printed?

03Copying out

A local copied out through the parameter does not write back

The editor can instead calculate a proposed count before committing it. This helper copies the integer out of bed, adds to that local and returns it. Predict the retained count and the proposed result.

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

import "fmt"

func addPlants(bed *int, extra int) int {
	count := *bed
	count += extra

	return count
}

func main() {
	bed := 12
	returned := addPlants(&bed, 4)

	fmt.Println(bed, returned)
}
What is printed?

04Re-pointing the parameter

Assigning to the parameter changes where it leads, not the caller’s value

A reduced harvest helper computes a full basket total, then tries to make its parameter name that local result. Check what that assignment means for the caller before building the retained harvest-log helper.

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

import "fmt"

func harvest(basket *int, picked int) {
	full := *basket + picked
	basket = &full

	fmt.Println("inside", *basket)
}

func main() {
	basket := 4
	harvest(&basket, 3)

	fmt.Println(basket)
}
What is printed?
Loading Changing a parameter target differs from writing through it…

05Repair the garden

Seedlings, baskets, then every bed

Repair the planning-file import helpers: add seedlings to a retained bed record, append picked quantities to a harvest record, then apply bulk additions and between-record transfers. All quantities describe the software’s plan and inventory records.

The supplied caller retains and prints the records after each import. A move between the same record changes nothing; a move beyond the recorded source count keeps the resulting deficit in the plan. Use the permitted native edit regions, including both Plant and plantAll in Go.

Repair the garden

Update one count, retain a harvest log’s appended entry, then apply bulk planting and transfers to the stored beds.

  1. easy · F-PARAM-V4-1 Add seedlings to a bed
  2. medium · F-PARAM-V4-2 Fill the harvest basket
  3. hard · F-PARAM-V4-3 Plant every bed
In the frontend Store a returned count through the framework’s state API. Optional

A browser count editor can use a helper that takes an ordinary number and returns a proposed count. The event handler must store that return value. The helper’s local integer assignment alone would leave the displayed state unchanged.

Example.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
"use client";

import { useState } from "react";

function addPlants(bed: number, extra: number) {
  return bed + extra;
}

export default function BedCount() {
  const [plants, setPlants] = useState(12);

  return (
    <button onClick={() => setPlants((current) => addPlants(current, 4))}>
      Planned: {plants}
    </button>
  );
}

The updater receives the pending state and returns its replacement. addPlants only computes a number; setPlants schedules the state update and render.

Sources: React: useState updater functions,Svelte: $state and function arguments.

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.