Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Multiple Return Values | Functions
Introduction to GoLang

bookMultiple Return Values

Similar to passing multiple arguments into a function, we can also retrieve multiple data values from the function.

The syntax for defining a function with multiple return values is as follows:

func myFunc(param_1 int, ...) (datatype_1, datatype_2, ...) {
      return expression_1, expression_2, ...
}

Below is an example of a function that returns two distinct values:

index.go

index.go

copy
12345678910111213141516171819202122
package main import "fmt" func getGroup(n int) string { if (n % 2 == 0) { return "Even" } else { return "Odd" } } func evaluateNumber(n int) (int, string) { var square int = n*n var group string = getGroup(n) return square, group } func main() { fmt.Println(evaluateNumber(5)) // Outputs: 25 Odd }

The returned values can be stored using the following syntax:

index.go

index.go

copy
1234
// Syntax: var variable_1, variable_2, ... = myFunc(...) var val_1, val_2 = evaluateNumber(5) fmt.Println("Square:", val_1) fmt.Println("Group:", val_2)
question mark

Complete the following code by selecting an appropriate answer:

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 5

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 1.96

bookMultiple Return Values

Desliza para mostrar el menú

Similar to passing multiple arguments into a function, we can also retrieve multiple data values from the function.

The syntax for defining a function with multiple return values is as follows:

func myFunc(param_1 int, ...) (datatype_1, datatype_2, ...) {
      return expression_1, expression_2, ...
}

Below is an example of a function that returns two distinct values:

index.go

index.go

copy
12345678910111213141516171819202122
package main import "fmt" func getGroup(n int) string { if (n % 2 == 0) { return "Even" } else { return "Odd" } } func evaluateNumber(n int) (int, string) { var square int = n*n var group string = getGroup(n) return square, group } func main() { fmt.Println(evaluateNumber(5)) // Outputs: 25 Odd }

The returned values can be stored using the following syntax:

index.go

index.go

copy
1234
// Syntax: var variable_1, variable_2, ... = myFunc(...) var val_1, val_2 = evaluateNumber(5) fmt.Println("Square:", val_1) fmt.Println("Group:", val_2)
question mark

Complete the following code by selecting an appropriate answer:

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 5
some-alt