Introduction
In Go, functions can return values to the caller. This allows functions to compute results and pass them back for further use. Let's explore how to define functions with return values and how to handle them in our code.
Return Values
When a function needs to return a value, it must declare the type of the return value in its signature and use the return
keyword to send the result back to the caller.
Function Return Example
Consider a function add
that takes two integers as parameters and returns their sum as an integer.
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(1, 2))
}
Output
3
In this example, the add
function calculates the sum of two integers and returns the result using the return
keyword. The main
function calls add(1, 2)
and prints the returned value.
Named Return Values
In Go, you can name the return values of a function, making the code more readable.
package main
import "fmt"
func add(x int, y int) (result int) {
result = x + y
return
}
func main() {
fmt.Println(add(1, 2))
}
Output
3
The example above can also be written using a named return value directly in the return statement:
package main
import "fmt"
func add(x int, y int) (result int) {
result = x + y
return result
}
func main() {
fmt.Println(add(1, 2))
}
Store Return Value in a Variable
You can store the return value of a function in a variable for later use.
package main
import "fmt"
func add(x int, y int) (result int) {
result = x + y
return
}
func main() {
total := add(1, 2)
fmt.Println(total)
}
Multiple Return Values
Go functions can return multiple values, which can be useful in various scenarios.
package main
import "fmt"
func processInput(input int, message string) (result int, outputMessage string) {
result = input * 2
outputMessage = message + " World!"
return
}
func main() {
fmt.Println(processInput(5, "Hello"))
}
Output
10 Hello World!
In this example, the processInput
function returns an integer (result
) and a string (outputMessage
). Both values are returned and printed in the main
function.
Omitting Returned Values
If we don't need to use all the returned values, we can use an underscore (_
) to omit them.
package main
import "fmt"
func processInput(input int, message string) (result int, outputMessage string) {
result = input * 2
outputMessage = message + " World!"
return
}
func main() {
_, message := processInput(5, "Hello")
fmt.Println(message)
}
Output
Hello World!
In this example, we omit the first returned value (result
) and only use the second one (outputMessage
) in the main
function.
Conclusion
Functions in Go can return values to the caller, providing a way to compute results and pass them back for further use. By understanding how to define functions with return values and handle them in our code, we can write more flexible and powerful programs. Experiment with named return values, multiple return values, and omitting returned values to fully leverage the capabilities of Go functions.