Golang functions have special functionality that allows them to provide the name to the return values. These named return values can be used as arguments or variables. The named return values also use the default values for the data types like 0 for int type etc. To understand this concept let’s take an example:
package main
import "fmt"
func courses(numbers [] int ) (dsa int , placement int ) {
if len(numbers) >= 2 {
dsa = numbers[0]
placement = numbers[len(numbers)-1]
}
return dsa, placement
}
func main() {
fmt.Println( "Displaying Default Values For Named Return Values" )
prices := [] int {}
fmt.Println(courses(prices))
fmt.Println()
fmt.Println( "Displaying Assigned Values For Named Return Values" )
prices = [] int {2499, 7499}
fmt.Println(courses(prices))
}
|
Example:
Displaying Default Values For Named Return Values
0 0
Displaying Assigned Values For Named Return Values
2499 7499
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 May, 2020
Like Article
Save Article