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.
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)
}
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.
package main
import "fmt"
func addPlants(bed *int, extra int) {
*bed += extra
}
func main() {
bed := 12
addPlants(&bed, 4)
fmt.Println(bed)
}
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.
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)
}
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.
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)
}
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.
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.
"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
Sign in to keep your read marks on every device. Sign in
Was this helpful?