In the Go programming language, functions are capable of returning values, which allows us to capture and use those results elsewhere in our code. To enable this, we define the return type when declaring the function and use the return
keyword inside the function to specify what will be sent back.
Basic Function Return
When creating a function that returns a value, we specify the expected data type in the function definition.
Syntax:
func functionName(parameter1 type, parameter2 type) returnType {
// logic here
return value
}
Example:
In this example, computeMileage()
receives two integer values representing initial and additional mileage, then returns their sum.
package main
import ("fmt")
func computeMileage(initialMileage int, addedMileage int) int {
return initialMileage + addedMileage
}
func main() {
fmt.Println(computeMileage(15000, 500))
}
Output:
15500
Named Return Values
Go allows us to name return values directly in the function signature. This makes the code more readable.
Example:
Here, the function calculateTotalPrice()
has a named return variable totalCost
, which is assigned a value and returned without explicitly specifying it in the return statement.
package main
import ("fmt")
func calculateTotalPrice(price int, tax int) (totalCost int) {
totalCost = price + tax
return
}
func main() {
fmt.Println(calculateTotalPrice(25000, 3500))
}
Output:
28500
We can also explicitly return the named variable:
package main
import ("fmt")
func calculateTotalPrice(price int, tax int) (totalCost int) {
totalCost = price + tax
return totalCost
}
func main() {
fmt.Println(calculateTotalPrice(25000, 3500))
}
Output:
28500
Storing Returned Values in Variables
Instead of printing the return value directly, we can store it in a variable for further use.
Example:
Here, we store the total cost returned by calculateTotalPrice()
in a variable called finalAmount
.
package main
import ("fmt")
func calculateTotalPrice(price int, tax int) (totalCost int) {
totalCost = price + tax
return
}
func main() {
finalAmount := calculateTotalPrice(25000, 3500)
fmt.Println(finalAmount)
}
Output:
28500
Multiple Return Values
Functions in Go can return more than one value, which is useful when we need multiple outputs.
Example:
In this example, motorcycleDetails()
returns both a price and a brand name.
package main
import ("fmt")
func motorcycleDetails(price int, brand string) (finalPrice int, fullName string) {
finalPrice = price + 3000
fullName = brand + " Performance"
return
}
func main() {
fmt.Println(motorcycleDetails(18000, "Yamaha"))
}
Output:
21000 Yamaha Performance
We can also store the returned values into separate variables:
package main
import ("fmt")
func motorcycleDetails(price int, brand string) (finalPrice int, fullName string) {
finalPrice = price + 3000
fullName = brand + " Performance"
return
}
func main() {
modelPrice, brandName := motorcycleDetails(18000, "Yamaha")
fmt.Println(modelPrice, brandName)
}
Output:
21000 Yamaha Performance
Omitting Unused Returned Values
If we only need one of the returned values, we can use an underscore (_
) to discard the unused one.
Example:
Omitting the price and keeping the brand name:
package main
import ("fmt")
func motorcycleDetails(price int, brand string) (finalPrice int, fullName string) {
finalPrice = price + 3000
fullName = brand + " Performance"
return
}
func main() {
_, brandName := motorcycleDetails(18000, "Yamaha")
fmt.Println(brandName)
}
Output:
Yamaha Performance
Omitting the brand name and keeping the price:
package main
import ("fmt")
func motorcycleDetails(price int, brand string) (finalPrice int, fullName string) {
finalPrice = price + 3000
fullName = brand + " Performance"
return
}
func main() {
modelPrice, _ := motorcycleDetails(18000, "Yamaha")
fmt.Println(modelPrice)
}
Output:
21000