Naming the Return Values of a Function in Golang

A return value helps to retain the final output of the function after it performs the instructions given in its body. Functions in Golang exhibit variety in return values and rely on the programmer to decide whether to name them or not.
Golang introduces a concept of “Naked Return” allowing the use of return keyword without explicitly stating the return values in the function body provided that the return values are declared in the function header. However, the variable name must be the same as the one defined in the function header.

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import "fmt"
  
// This function returns a string message by taking a parameter
// "name" of string type. The Naked return concept is being
// displayed here since the return statement not specify the return
// of message variable. The returning of the message variable
// has already being defined 
func greeting(name string) (message string) {
  
    // Since the variable is already 
    // defined in function variable
    // there is no need to declare it 
    // again in the function body
    // using " := " operator.
  
    // The name "message" must be same as 
    // the one declared in the function header.
    message = "Hello, " + name
      
    // This statement returns message variable
    // without explicitly stating its name
    return 
}
  
// Driver code
func main() {
  
    // The return value of greeting 
    // function is stored in msg variable
    msg := greeting("GFG")
      
    // Printing output
    fmt.Println(msg) 
}

chevron_right


Output:

Hello, GFG

Also, the function can return a variable without specifying its name in the function header but only its datatype.

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import "fmt"
  
// This function returns a string datatype
// Since the return variable is not 
// defined in the function header,
// There is an explicit need to specify
// the variable to return
  
func greeting(name string) string {
    message := "Hello, " + name
    return message
}
  
// Driver code
func main() {
    msg := greeting("GFG")
    fmt.Println(msg)
}

chevron_right


Output:

Hello, GFG

A function can also return multiple values and receive multiple parameters. However, to work with a function that returns multiple values, the number of variables that the function returns must be equal to the number of variables that hold the return values when the function execution finishes.

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import "fmt"
  
// The below function returns four
// variables after performing operations
// on the two parameters that were passed to it
  
func operations(a, b int) (sum, diff, prod, div int) {
  
    // The four variables to be returned are defined here
  
    sum, diff, prod, div = a+b, a-b, a*b, a/b
  
    // returning the above-defined four variables.
    return
}
  
// Driver code
func main() {
    a, b := 5, 3
  
    // Since the function returns four values,
    // Four receiver variables must be used.
    sum, diff, prod, div := operations(a, b)
    fmt.Println(sum, diff, prod, div)
}

chevron_right


Output:

8 2 15 1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.