Named Return Parameters in Golang (original) (raw)

Last Updated : 12 Jul, 2025

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

// function having named arguments func calculator(a, b int) (mul, div int) {

// 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

}