DEV Community

Cover image for Go Basics: Functions
Abhinav Pandey
Abhinav Pandey

Posted on

Go Basics: Functions

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 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() {
Enter fullscreen mode Exit fullscreen mode
  • The function is declared with the return type int and the function name square.
  • It has one parameter a of type int.
  • 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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 {
    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))

Enter fullscreen mode Exit fullscreen mode

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.

Oldest comments (0)