Return Multiple Values from Function in Golang



In this tutorial, we are going to see how we can return multiple values from the function in Golang with the help of an algorithm and examples. Like other programming languages like python, Golang also supports returning multiple values from a function. In the first, example we are going to pass two numbers in the function and return the smaller and then greater number together. In the second example we are passing two numbers and returning the addition, subtraction, multiplication, and division at once.

Syntax

Func functionName(arguments) (returnType1, returnType2, ?) {

   // logic
   return returnTypeVariable1, returnTypeVariable2, ?
}

Explanation

(returnType1, returnType2, ?) - In this line of code in place to returnType1 and, returnType2 we can write the data types of our return values.

return returnTypeValue1, returnTypeValue2, ? - Here we are returning the variables of respective data types.

Algorithm

  • Step 1 ? Declare the variables.

  • Step 2 ? Initialize the variables.

  • Step 3 ? Call the function that returns multiple values.

  • Step 4 ? Print the result.

Example 1

In this example, we will call a function with two integer numbers as arguments. This function will return the smaller number first follower by the bigger number.

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)
func smallerNumber(number1, number2 int) (int, int) {
   if number1 < number2 {
      return number1, number2
   }
   return number2, number1
}
func main() {

   // declaring the variable
   var number1, number2 int
   
   // initializing the variable
   number1 = 10
   number2 = 21
   fmt.Println("Golang program to return the multiple values from the function in Golang.")
   
   // calling the recursive function
   smaller, bigger := smallerNumber(number1, number2)
   fmt.Println("The smaller number is", smaller)
   fmt.Println("The bigger number is", bigger)
}

Output

Golang program to return the multiple values from the function in Golang.
The smaller number is 10
The bigger number is 21

Example 2

In this example, we will write a function that will return the addition, subtraction, multiplication, and division at once. This approach will lead to reducing the number of function calls in a program.

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)
func addSubMulDiv(number1, number2 int) (int, int, int, int) {
   var addition, subtraction, multiplication, division int
   
   // adding two numbers
   addition = number1 + number2
   
   // subtracting two numbers
   subtraction = number1 - number2
   
   // multiplying two numbers
   multiplication = number1 * number2
   
   // dividing two numbers
   division = number1 / number2
   return addition, subtraction, multiplication, division
}
func main() {

   // declaring the variable
   var number1, number2 int
   
   // initializing the variable
   number1 = 100
   number2 = 20
   fmt.Println("Golang program to return the multiple values from the function in Golang.")
   
   // calling the recursive function
   addition, subtraction, multiplication, division := addSubMulDiv(number1, number2)
   fmt.Printf("The addition of %d and %d is %d.\n", number1, number2, addition)
   fmt.Printf("The subtraction of %d and %d is %d.\n", number1, number2, subtraction)
   fmt.Printf("The multiplication of %d and %d is %d.\n", number1, number2, multiplication)
   fmt.Printf("The division of %d and %d is %d.\n", number1, number2, division)
}

Output

Golang program to return the multiple values from the function in Golang.
The addition of 100 and 20 is 120.
The subtraction of 100 and 20 is 80.
The multiplication of 100 and 20 is 2000.
The division of 100 and 20 is 5.

Conclusion

This is the way to return multiple values from the function in Golang with two examples. To learn more about Golang you can explore these tutorials.

Updated on: 2023-01-11T17:13:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements