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.
- Return value
- The value a call gives back; it takes the place of the call in the caller’s expression.
- Multiple results
- Two or more values given back together, as a tuple, a Go result list, a pair or a record.
- Every path
- A way through a function’s branches. Each normally returning path must meet its result contract; paths that throw or diverge are different.
- Success result
- A second result, or an Option/optional, that says whether the main result is real.
- Comma-ok
- Go’s value, ok := f() pattern, where ok reports whether value is meaningful.
Results are a call contract
Returning data and performing an action are separate responsibilities. A helper can print a diagnostic without supplying the value its caller needs. Its declared result and every reachable return path must agree with how the call is used; the selected language determines which incomplete contracts it rejects.
When a call supplies several parts, their order or field names carry meaning as well as their types. Two integer parts can have different responsibilities even though exchanging them remains type-correct. Check what each result represents at both the return site and the receiving caller.
01A function without a result
Does the audit function provide a stored result?
A parcel desk is checking its dispatch preview before staff rely on it. The audit helper was written for logging. The preview also needs data back from its helpers: a band for each weight, full-box and loose-item counts, and a total that excludes unreadable order lines. Follow the result each caller actually receives.
The audit helper record prints a line. The preview then tries to store and print the call’s result. Inspect its declaration and body, and predict whether that caller is supported.
record has no result list; the call is assigned with :=.
package main
import "fmt"
func record(entry string) {
fmt.Println("saved", entry)
}
func main() {
result := record("order 7")
fmt.Println(result)
}
go run .
Use the selected language’s setup from the first reading and replace the file for each experiment.
02Every path
A path that reaches the closing brace gives back undefined, a compile error or undefined behaviour
The preview now asks for a parcel band. This reduced helper covers two bands and leaves its final path unfinished. Compare the caller inputs in each native version before predicting the build result and any defined output.
boxLabel declares a string result; main calls it with 4 and 40.
package main
import "fmt"
func boxLabel(weight int) string {
if weight < 10 {
return "small"
}
if weight < 30 {
return "medium"
}
}
func main() {
fmt.Println(boxLabel(4))
fmt.Println(boxLabel(40))
}
03Two results
Two results come back by position, whatever the caller names them
Packing will require two integer result parts at the call site. First isolate positional binding with a small low/high price probe: the caller receives the results into variables named high and low. Predict both lines before attaching meaning to those local names.
lowHigh has named results (low, high int); the caller uses :=.
package main
import (
"fmt"
"slices"
)
func lowHigh(prices []int) (low, high int) {
low = slices.Min(prices)
high = slices.Max(prices)
return low, high
}
func main() {
high, low := lowHigh([]int{12, 4, 9})
fmt.Println("high", high)
fmt.Println("low", low)
}
04Repair the parcel desk
Band every parcel, count boxes in order, skip unreadable lines
The dispatch preview needs a band for every supplied weight and a packing result whose two parts mean full boxes and loose items. It also reads order quantities before making a total. The supplied quantity reader carries success separately: TypeScript and Go use a boolean result, while Rust and C++ use an optional value. A readable zero and an unreadable word must remain distinguishable.
Each file contains its imports, reader and complete main. Supplied weights are 0 to 1000 whole kilograms. Packing inputs have 0 to 1000 items and a positive box size up to 1000. Quantity words use ASCII decimal digits, with leading zeros allowed; signs, fractions and suffixes are unreadable. Supplied numeric quantities are 0 to 100 and there are at most 100 lines, keeping native totals in range. The readers do not enforce these supplied-data bounds.
Repair the parcel desk
A result on every path, results in the order the caller reads them, then a second result that says whether the first is real.
In the frontend A helper return must be consumed by the markup. Optional
The dispatch UI needs a weight label as data. A helper returns that text, and the component explicitly puts it in an output element. Logging the text would be a separate effect and would not supply this rendered content.
function weightText(weight: number): string {
return `${weight} kg`;
}
export function Weight({ weight }: { weight: number }) {
const text = weightText(weight);
return <output>{text}</output>;
}
The helper returns a string; the component returns JSX that includes that string.
Sources: React: JavaScript in JSX with curly braces,Svelte: Basic markup.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?