By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Some answers are not one value. Splitting 1250 cents into dollars and cents gives two numbers. Go lets a function return several values, listed in the signature after the parameter list.
The caller receives them with a matching list of variables and the := operator. This is one of the places where Go feels different from languages that return a single object or throw an exception: multiple results are part of the function's type.
package main
import "fmt"
func splitCents(cents int) (int, int) {
dollars := cents / 100
remainder := cents % 100
return dollars, remainder
}
func main() {
dollars, remainder := splitCents(1250)
fmt.Println(dollars, "dollars and", remainder, "cents")
dollars, remainder = splitCents(4299)
fmt.Println(dollars, "dollars and", remainder, "cents")
whole, _ := splitCents(999)
fmt.Println("whole dollars:", whole)
}(int, int) in the signature is the result list. return dollars, remainder sends both values in the order they are written. On the caller side, dollars, remainder := splitCents(1250) declares two variables at once. Use _ for a result you do not need; the blank identifier means throw this one away.
The most common pair in Go is a value and an error. A function either succeeds and returns the value with a nil error, or fails and returns a zero value with a real error. The caller checks the error before trusting the value.
package main
import (
"errors"
"fmt"
)
func divide(total, parts int) (int, error) {
if parts == 0 {
return 0, errors.New("parts must not be zero")
}
return total / parts, nil
}
func main() {
share, err := divide(1250, 5)
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Println("each share:", share)
}
share, err = divide(1250, 0)
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Println("each share:", share)
}
}In the success branch, err is nil and share is meaningful. In the failure branch, err explains what went wrong and share is a placeholder 0. Go has no exceptions to catch: errors are ordinary values you inspect with if. That is why step 30 of this course is dedicated to them.
You can name the results in the signature. Named results are variables declared inside the function: the body assigns them and returns them. The names document what each value means, and they show up in editor hints as you use the function.
A short function can also finish with a bare return that sends the current values of the named results. This course writes the values explicitly anyway, because a plain return avg, ok is easier to follow once a function grows past a screen.
package main
import "fmt"
func average(total, count int) (avg int, ok bool) {
if count == 0 {
return 0, false
}
avg = total / count
ok = true
return avg, ok
}
func main() {
value, ok := average(5000, 4)
fmt.Println("average:", value, "ok:", ok)
value, ok = average(5000, 0)
fmt.Println("average:", value, "ok:", ok)
}The scope of a name is the part of the program where it is visible. Three levels cover everything in this section: package scope, function scope and block scope.
Package scope names are declared outside every function: other functions, package-level variables and constants. Any function in the file can use them. Function scope names are the parameters and local variables. They exist only while that call is running. Block scope names are declared inside if, for or switch braces and disappear at the closing brace.
Name lookup works outward. Go checks the current block first, then the surrounding function, then package scope. The first match wins, which is why a more specific name can hide a more general one.
If a block declares a name that already exists one level out, the inner name shadows the outer one. Inside the block the outer value is invisible. Outside the block the outer value is unchanged.
package main
import "fmt"
func describe(cents int) {
fmt.Println("outer cents:", cents)
if cents > 1000 {
cents := cents * 2
fmt.Println("inner cents:", cents)
}
fmt.Println("outer cents still:", cents)
}
func main() {
describe(2500)
}Write func withdraw(balance, amount int) (int, error). Return errors.New("amount must be positive") when the amount is zero or negative, errors.New("insufficient funds") when it is larger than the balance, and otherwise the new balance with nil. In main, call it with (5000, 1250) and with (5000, 9000), checking err before printing. Expected: the first call prints remaining: 3750, the second prints the insufficient funds error. Then add a temporary balance := 0 inside an if block and confirm the outer balance keeps its value.
Key takeaways
A function can return several values: (int, int) or (int, error).
The (value, error) shape means check err first, then use the value.
dollars, remainder := splitCents(...) declares several variables at once; _ ignores one.
Named results are declared in the signature, assigned in the body and returned to the caller.
Scope is where a name lives: block, function, package. An inner block can shadow an outer name.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.