

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Functions
incomplete
2: Multiple Parameters
incomplete
3: Unit Test Lessons
incomplete
4: Declaration Syntax
incomplete
5: Passing Variables by Value
incomplete
6: Ignoring Return Values
incomplete
7: Named Return Values
incomplete
8: The Benefits of Named Returns
incomplete
9: Explicit Returns
incomplete
10: Early Returns
incomplete
11: Functions As Values
incomplete
12: Anonymous Functions
incomplete
13: Defer
incomplete
14: Block Scope
incomplete
15: Processing Orders
incomplete
16: Closures
incomplete
17: Currying
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Return values may be given names, and if they are, then they are treated the same as if they were new variables defined at the top of the function.
Named return values are best thought of as a way to document the purpose of the returned values.
According to the tour of go:
A return statement without arguments returns the named return values. This is known as a "naked" return. Naked return statements should be used only in short functions. They can harm readability in longer functions.
Named return values are what enable naked returns. Use naked returns only in short functions where the purpose of the returned values is obvious.
func getCoords() (x, y int) {
// x and y are initialized with zero values
return // automatically returns x and y
}
Is the same as:
func getCoords() (int, int) {
var x int
var y int
return x, y
}
In the first example, x and y are the return values. At the end of the function, we could simply write return to return the values of those two variables, rather than writing return x,y.
One of our clients likes us to send text messages reminding users of life events coming up.
Fix the bug by adding named return values to the function signature – the bare return at the end is already a naked return that will return them. The variables need to be automatically initialized. Order them as they appear in the code. Do not alter the body of the function.