Open In App

Golang Program that Uses Multiple Return Values

Last Updated : 23 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Golang, we can return multiple values at a time from a single function. Multiple return values can be achieved by changing the return type of the function in the function signature.

Syntax : 

func value( ) ( int , int ) { 
return 1 , 0 ; 
} 

The (int, int) in this function signature explains that the return type is two integers. Thus we have to use multiple assignments while calling this function. This feature is also used to return both results and error from a function. If you want a subset of the returned values, use the blank identifier _.

Example 1: Finding max and min of two elements, using this feature : 

Go




package main
 
import "fmt"
 
// declaring a function
// having return type
// of int, int
func maxmin(a int, b int) (int, int) {
 
    if a > b {
     
        // separate multiple return
        // values using comma
        return a, b
    } else {
 
        return b, a
    }
    // this function returns
    // maximum , minimum values
}
 
func main() {
 
    // declaring two values a and b
    var a = 50
    var b = 70
 
    // calling the function
    // with multiple assignments
    var max, min = maxmin(a, b)
 
    // Printing the values
    fmt.Println("Max = ", max, "\nMin = ", min)
}


Output : 

Max =  70 
Min =  50

Example 2: Finding the sum and difference of two numbers, using this feature of multiple return values.

Go




package main
 
import "fmt"
 
// declaring a function having
// return type of int, int
func sumDiff(a int, b int) (int, int) {
 
    return (a + b), (a - b)
 
    // this function returns sum ,
    // difference of the two numbers
}
 
func main() {
 
    // declaring two values a and b
    var a = 68
    var b = 100
 
    // calling the function
    // with multiple assignments
    var sum, diff = sumDiff(a, b)
 
    // Printing the values
    fmt.Println("Sum = ", sum, "\nDifference = ", diff)
}


Output : 

Sum =  168 
Difference =  -32


Similar Reads

Golang Program that Uses Named Return Values and Defaults
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: // Golang Program that Uses Named //
1 min read
Golang program that uses switch, multiple value cases
Switch statement is a multiway branching which provides an alternative way too lengthy if-else comparisons. It selects a single block to be executed from a listing of multiple blocks on the basis of the value of an expression or state of a single variable. A switch statement using multiple value cases correspond to using more than one value in a si
3 min read
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 r
3 min read
Golang program that uses fallthrough keyword
With the help of fallthrough statement, we can use to transfer the program control just after the statement is executed in the switch cases even if the expression does not match. Normally, control will come out of the statement of switch just after the execution of first line after match. Don't put the fallthrough in the last statement of switch ca
2 min read
Golang program that uses structs as map keys
A map in Golang is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update or delete with the help of keys. Syntax: map[Key_Type]Value_Type{} Example: var sample map[string]int Here the sample is a map that has a string as key and int type as the value. In maps, most of t
5 min read
Golang program that uses func with two arguments
Functions may use certain parameters to receive values and perform the task required accordingly. The syntax for a function with two arguments is: func function_name( [parameter_1 parameter_2] ) [return_types] { body of the function } Example 1: Without any return type // Golang program that uses func with // two arguments without any return type p
1 min read
Golang program that uses defer keyword
The main use of the defer keyword in go is that it is used to clean up the resources used by opened files, network connections, database handles, etc. This helps to reduce the chances of error by keeping the call to close function/files nearer to call to open statements. The statement that is followed by defer keyword is placed onto a stack and in
2 min read
Golang Program that uses func as Local Variable
Function performs a specific task, section of code that defined once can be reused. Functions are used to make your code easier to understand by breaking it into small and understandable tasks.Functions are also known as method, sub-routine, or procedure. General form of a function definition in Go programming language is given below- func function
3 min read
Golang program that uses func with variable argument list
In Golang, a function that can be called with a variable argument list is known as a variadic function. One can pass zero or more arguments in the variadic function. If the last parameter of a function definition is prefixed by ellipsis ..., then the function can accept any number of arguments for that parameter. Syntax of a variadic function: func
2 min read
Golang Program that uses String Switch
With the help of switch case we can implement the functionality of as many if statements. In Golang, switch cases can work with strings, list of variables including integer values as well as floating values. Syntax: switch optstatement; optexpression{case expression1: Statement..case expression2: Statement..…default: Statement..}Example 1: In this
2 min read
Article Tags :