Go · Chapter 4 of 26
Functions
By the end of this chapter you can write your own Go functions with parameters and results, return several results at once, take any number of arguments, pass functions around as values, write a closure that remembers its variables, and build a chapter's files together into one program with go run .. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The language
- 4 tasks
- Go
- In the all labs plan
The first 2 chapters of Go are free. This one comes with the all labs plan.
Why you need this
Pooja found the same bill worked out four different ways. The welcome program, the bill program, the night report and one of Tenzin's scripts each had their own copy of "watt-hours times tariff, divided by 1000". One copy rounded to the nearest paisa, two cut the fraction off, and one multiplied in the wrong order and lost a digit. On Wednesday's 3,600 sessions, every copy gave a slightly different total.
With what you know so far, every program carries its own copy of every formula, and fixing one copy leaves the others wrong. A function gives a piece of work a name, so it is written once, tested once, and every program calls the same one. Go's functions also do two things many languages need extra machinery for: they return several results at once, and they can be passed around and remember values like any other value.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
cost.go: a session's cost, to the nearest paisa
Write the function Cost(wh int, tariff int) int in cost.go. It takes a session's energy in watt-hours and the tariff in paise a kWh, and returns the cost in whole paise: wh times tariff divided by 1000, rounded to the nearest paisa, with a half rounding up. Keep the money in ints, with no float64.
The Check calls Cost(17385, 2000) and expects 34770, Cost(4312, 1400) 6037 (exactly 6036.8), Cost(5333, 1400) 7466 (exactly 7466.2), Cost(1, 500) 1 (exactly 0.5), Cost(0, 2400) 0, Cost(11228, 900) 10105 and Cost(9216, 2400) 22118.
- Task 2
split.go: rupees and paise, from one function
Write the function Split(paise int) (int, int) in split.go. It takes an amount in paise and returns two results: the whole rupees, then the paise left over.
The Check calls Split(36874) and expects 368, 74; Split(5) 0, 5; Split(100) 1, 0; Split(0) 0, 0; and Split(83158800) 831588, 0.
- Task 3
total.go: the total of any number of amounts
Write the function Total(amounts ...int) int in total.go. It takes any number of amounts in paise, including none, and returns their sum.
The Check calls Total(34770, 7466, 6037) and expects 48273, Total(6037) 6037, Total() 0, and Total(100, 200, 300, 400, 500, 600) 2100.
- Task 4 · interview question
Interview: counter.go, a closure that counts
Write the function NewCounter() func() int in counter.go. It returns a new function that counts heartbeats: each call of the returned function adds one and returns how many it has counted so far. Each counter starts at 0 and counts on its own, so the count must belong to the counter, not to a variable outside NewCounter.
The Check makes c := NewCounter() and expects c() to return 1, 2 and 3 on three calls; then d := NewCounter() and expects d() to return 1; then c() again must return 4. A third counter called 1180 times must return 1180 the last time.
Joseph will ask: what does the function you return capture, and where does the count live after NewCounter returns?
What you will learn
- func: parameters, a result, and return.
- Several results: h, m := HoursMinutes(157), and the blank identifier _.
- Named results: in one paragraph.
- Variadic functions: Lowest(watts ...int) takes any number of values.
- Functions as values, and closures that capture variables.
- One package, many files: why the chapter now has main.go and go run ..
More in The language
Build "Functions" yourself
BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go are free. This one comes with the all labs plan.