Open In App

Named Return Parameters in Golang

Last Updated : 25 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Functions in Golang
In Golang, Named Return Parameters are generally termed as the named parameters. Golang allows giving the names to the return or result parameters of the functions in the function signature or definition. Or you can say it is the explicit naming of the return variables in the function definition. Basically, it eliminates the requirement of mentioning the variables name with the return statement. By using named return parameters or named parameters one can only use return keyword at the end of the function to return the result to the caller. This concept is generally used when a function has to return multiple values. So for the user comfort and to enhance the code readability, Golang provide this facility. 
 

Declaring the Named Return Parameters

To declare the named result or return parameters, just use the return type part of the function signature. Below is the general syntax to declare a function in Golang.
Syntax to declare a function without named return arguments:
 

func function_name(Parameter-list)(Return_type){
    // function body.....
}

Here, the Return_Type is optional and it contains the types of values that function returns. If you are using Return_Type in your function, then it is necessary to use a return statement in your function.
Syntax to declare a function with named return arguments:
 

func function_name(Parameter-list)(result_parameter1 data-_type, result_parameter2 data_type, ….){ 
// function body…..
return 

 

Here, (result_parameter1 data-_type, result_parameter2 data_type, ….) is the named return argument list along with their type. You can declare n number of named return parameters.
 

Named-Return-Parameters-Golang1

Example: In the below program, the func calculator(a, b int) (mul int, div int) line of code contains the named return arguments. The return statement at the end of function doesn’t contain any parameters. Go compiler will automatically returns the parameters.
 

C




// Golang program to demonstrate the
// use of Named Return Arguments
 
package main
 
import "fmt"
 
// Main Method
func main() {
 
    // calling the function, here
    // function returns two values
    m, d := calculator(105, 7)
 
    fmt.Println("105 x 7 = ", m)
    fmt.Println("105 / 7 = ", d)
}
 
// function having named arguments
func calculator(a, b int) (mul int, div int) {
 
    // here, simple assignment will
    // initialize the values to it
    mul = a * b
    div = a / b
 
    // here you have return keyword
    // without any resultant parameters
    return
}


Output:
 

105 x 7 =  735
105 / 7 =  15

 

Important Points

 

  • If the type of all the named return arguments is common or same then you can specify the common data type. Compare the below code with the example that you read above to get a better understandability.
     
// function having named arguments
func calculator(a, b int) (mul, div int) {
  • Here, mul and div variables are both int type. So you can also declare named arguments with common data type like the function variables(i.e. a and b)
  • Using Named return parameters will enhance the code readability as one can know about the return parameters by just reading the function signature.
  • After using named return parameters the return statement is generally termed as Naked or Bare return.
  • By default, Golang defines all the named variables with the zero value and function will able to use them. In case function doesn’t modify the values then automatically zero value will return.
  • If you will use short declaration operator(:=) to initialize the named return parameters, it will give an error as they are already initialized by the Go compiler. So you can use simple assignment(=) to assign the values to named return parameters.
     
// function having named arguments
func calculator(a, b int) (mul int, div int) {

    // here, it will give an error
        // as parameters are already defined
        // in function signature
    mul := a * b
    div := a / b

    // here you have return keyword
    // without any resultant parameters
    return
}
  • Named return arguments or bare return statements are only good for the short function signature. For longer functions, explicitly return the result parameters(not use named return parameters) to maintain the readability of the code.
  • In the case of named return arguments, bare or naked return statement is a must.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads