×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to Implement Return Parameters in Golang?

Golang | Named Return Parameters: Write a Go language program to demonstrate the example of named return parameters.
Submitted by IncludeHelp, on October 22, 2021

In the Go programming language, the named return parameters are also known as the named parameters. Golang allows giving the names to the return or result parameters of the functions in the function signature or definition.

Syntax:

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

Example:

// Golang program to implement the
// Named Return Arguments

package main

import "fmt"

// function having named arguments
func CalculateSumSub(x, y int) (sum int, sub int) {
	// Assign the result to the arguments
	sum = x + y
	sub = x - y

	// return statement without any value
	return
}

// Main function
func main() {
	a, b := 10, 20
	res1, res2 := CalculateSumSub(a, b)

	fmt.Println("Sum:", res1)
	fmt.Println("Sub:", res2)
}

Output:

Sum: 30
Sub: -10

Golang FAQ »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.