Go Basics: Functions
Abhinav Pandey
Posted on November 25, 2021
In the last article, we discussed the syntax of variables and constants. Now let's look at the syntax of functions in Go and their variations.
Functions
Functions are declared using the func
keyword.
You may have seen the function syntax in the previous article. Let's revisit it.
// function that takes two integers and returns their sum
func square(a int) int {
return a*a
}
//main function calling square
func main() {
fmt.Println(square(2))
}
- The function is declared with the return type
int
and the function namesquare
. - It has one parameter
a
of typeint
. - The function body is enclosed in curly braces
{}
. - The return statement is used to return the value of the expression.
- The call to the function is simply
square(2)
- Similar to other languages.
Now let's look at some variations of the function syntax.
Function with multiple parameters
func add(a, b int) int {
return a + b
}
Since both parameters are of type int
, they can be declared comma separated.
Functions with different types of parameters
func add(a, b int, c float64) int {
return a + b + int(c)
}
The function accepts three parameters. The first two are of type int
and the last one is of type float64
.
When a parameter is of a different type than the next one, it is mandatory to declare its type.
Function with multiple return values
Unlike other languages like Java, it is possible to return multiple values from a function. To do so, we need to define the return type of all return values as a tuple.
func sumAndDiff(a, b int) (int, int) {
return a + b, a - b
}
//main function calling add
func main() {
sum, difference := sumAndDiff(10, 5)
fmt.Println(sum, difference)
}
Two return values are declared. Both of them are of type int
. Notice the return statement - it returns two values.
Notice how the function call is written as sumAndDiff(10, 5)
and two return values are assigned to the variables sum
and difference
.
Function with named return values
func sumAndDiff(a, b int) (sum int, difference int) {
sum = a + b
difference = a - b
return
}
The return values are declared with the names sum
and difference
. The return statement returns the values of the variables sum
and difference
but does not need to mention them.
This is useful when you are creating temporary variables to hold the return values till the end of the function.
Function with a variable number of parameters
func add(nums ...int) int {
sum := 0
//loop over nums and add them to sum
for _, num := range nums {
sum += num
}
return sum
}
//main function calling add
func main() {
fmt.Println(add(1, 2, 3, 4, 5))
}
The function accepts a variable number of parameters. The parameters are of type int
.
The ...
operator is used to indicate that the function accepts a variable number of parameters.
Notice how in the function call, we have sent 5 values.
Note: The _
in the for loop is a placeholder for the index of the number returned. We will talk more about for
loop variations later.
This should give an idea of function syntax in Go. Stay tuned for more on Go syntax.
Thanks for reading. If you want to connect with me, you can find me on Twitter @abh1navv.
Posted on November 25, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.