Flash cards
Review the key moves
What is the main idea behind Go Function Returns?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
___Put the learning moves in the order that makes the concept easiest to apply.
Return Values
If you want the function to return a value, you need to define the data type of the return value (such as int , string , etc), and also use the return keyword inside the function:
Syntax
func
FunctionName
( param1
type , param2
type )
type {
// code to be executed
return output
}Function Return Example
myFunction()Named Return Values
In Go, you can name the return values of a function.
resultThe example above can also be written like this. Here, the return statement specifies the variable name:
Example
package main
import ("fmt")
func myFunction(x int, y int) (result int) {
result = x + y
return result
}
func main() {
fmt.Println(myFunction(1, 2))
}Store the Return Value in a Variable
You can also store the return value in a variable, like this:
totalMultiple Return Values
Go functions can also return multiple values.
myFunction()aIf we (for some reason) do not want to use some of the returned values, we can add an underscore ( _ ), to omit this value.
resulttxt1