Functions can return a result. To do this, you need to specify the type of return result after the list of function parameters. And in the body of the function, use the return statement, followed by the return value:
func function_name(parameter_list) return_value type {
executable_statements
return_value
}
For example, we want to return the multiplication of two numbers from a function:
package main
import "fmt"
func main() {
var x = multi(23, 89)
fmt.Println(x) // 529
}
func multi(i, j int) int {
return i * i
}
The multi-function returns a value of type int, so this type is listed after the list of parameters. In the function itself, after the return statement, the returned value is specified. In this case, this value can be the value of a variable, a literal, or, as in this case, the result of an operation or a function call. That is, the expression defines the return value (i * j)
.
Since the function returns a value, when we call the function, we can get that value and pass it to the variable:
var x = multi(23, 89)
The returned result can be named:
func multi(a, b int)(r int) {
var int r = a * b
return r
}
In Go, a function can return multiple values at once. In this case, the list of return types is indicated in parentheses after the parameter list. After the return statement, All return values are separated by commas:
package main
import "fmt"
func main() {
var tutorials, website = info(1200, "techieclues", "learn Golang")
fmt.Println(tutorials)
fmt.Println(website)
}
func info(tutorials int, name, description string)(int, string) {
tutorials = tutorials * 2
website: = name + " " + description
return tutorials, website
}
Output:
2400
techieclues learn Golang
The info function takes three parameters: one number and two strings. Returns a number (an int value) and a string. Return values are specified after the return statement.
Since the function now returns two values, when we call this function, we can assign its result to two variables:
var tutorials, website = info(1200 , "techieclues", "learn Golang")
The first return value is passed to the first tutorials variable, and the second value is passed to the second website variable.
An alternative way to pass the results of a function to variables is as follows:
tutorials, website := info(1200 , "techieclues", "learn Golang")
Each function has a specific type, which is made up of a list of parameter types and a list of types of returned results. For example, let's take the following Function:
func a(b int, c int) int {
return b - c
}
This function represents the func(int, int) int type. The following function will correspond to the same type:
func x(y int, z int) int {
return y + z
}
Although this function has a different name, it performs different actions, but in terms of the type of parameters and the type of result returned, it corresponds to the above type of function.
Let's take another function:
func getName(name string) {
fmt.Println(name)
}
This function is of type func(string)
. That is, again, the word func comes first, followed by parameter types in parentheses. Since the function doesn't return any result, then the return type is not specified.
But what does the type of function matter? This means that we can define variables or parameters of functions that will represent a particular type of function. That is a variable can be a function. For example:
package main
import "fmt"
func x(y int, z int) int {
return y + z
}
func main() {
var a func(int, int) int = x
fmt.Println(a(10, 400))
}
Here, the variable a is of type func(int, int) int
, that is, it represents any function that takes two parameters of type int and returns a value of type int. Therefore, we can assign this variable an x function that corresponds to this type:
var a func (int int) = x
As a result, will get this output:
410
A function can also be passed as a parameter to another function. For example:
package main
import "fmt"
func sum(a int, b int) int {
return a + b
}
func sub(a int, b int) int {
return a - b
}
func do(i int, j int, todo func(int, int) int) int {
z: = todo(i, j)
fmt.Println(z)
return z
}
func main() {
do(200, 100, sum)
do(200, 100, sub)
}