Day 6 55 min beginner

Functions

Learn how to define and use functions, including Go's powerful multiple return values

Learning Objectives

  • Define and call basic functions
  • Work with parameters and return types
  • Return multiple values from a single function
  • Use named return values
  • Understand variadic functions

The Building Blocks of Go

Functions are central to Go. They allow you to group code into reusable units, making your programs more modular and easier to maintain.

Basic Function Syntax

Here’s how you define a function in Go:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

import "fmt"

// func Name(parameter list) return_type { ... }
func sayHello(name string) {
    fmt.Printf("Hello, %s!\n", name)
}

func main() {
    sayHello("Gopher")
}

Parameters and Return Values

If a function returns a value, you must specify the type after the parameters:

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

import "fmt"

func add(a int, b int) int {
    return a + b
}

func main() {
    result := add(10, 20)
    fmt.Println("Sum:", result)
}

Shorthand for Parameters

When consecutive parameters have the same type, you can omit the type for all but the last one:

1
2
3
4
5
6
7
func add(a, b int) int {
    return a + b
}

func complexOp(a, b int, name string, active bool) {
    // ...
}

Multiple Return Values

One of Go’s most distinctive and powerful features is the ability for a function to return multiple values. This is frequently used for returning both a result and an error.

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
    "errors"
    "fmt"
)

func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("cannot divide by zero")
    }
    return a / b, nil
}

func main() {
    result, err := divide(10, 2)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Result:", result)
    }
    
    // Using the blank identifier to ignore the error (not recommended!)
    val, _ := divide(10, 5)
    fmt.Println("Value:", val)
}

Named Return Values

Go’s return values can be named. If they are named, they are treated as variables defined at the top of the function. A return statement without arguments (called a “naked” return) will return the current values of the named return variables.

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package main

import "fmt"

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return // returns x and y
}

func main() {
    fmt.Println(split(17))
}
Use Named Returns for Clarity
Named return values are great for documenting the meaning of the return values, especially in functions with many returns. However, naked returns should only be used in short functions as they can harm readability in longer ones.

Variadic Functions

A variadic function can be called with any number of trailing arguments. fmt.Println is a common example. To define one, use ... before the type of the last parameter.

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import "fmt"

func sumAll(nums ...int) int {
    total := 0
    // nums is treated as a slice
    for _, num := range nums {
        total += num
    }
    return total
}

func main() {
    fmt.Println(sumAll(1, 2))
    fmt.Println(sumAll(1, 2, 3, 4, 5))
    
    // Passing a slice to a variadic function
    scores := []int{10, 20, 30}
    fmt.Println(sumAll(scores...))
}

Function Scope and Pass by Value

In Go, everything is passed by value. This means that when you pass a variable to a function, Go creates a copy of that variable. Changes made inside the function do not affect the original variable (unless you use pointers, which we’ll cover later).

main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package main

import "fmt"

func changeValue(val int) {
    val = 100 // only changes the local copy
}

func main() {
    x := 10
    changeValue(x)
    fmt.Println(x) // Still 10!
}

Summary

Today you learned:

  • Basic function definition and calling
  • Parameter shorthand syntax
  • How to return multiple values from a function
  • Using named return values for clarity
  • Writing variadic functions with ...
  • Understanding that Go passes everything by value

Practice Exercise

  1. Write a function calculate that takes two integers and returns their sum, difference, and product.
  2. Create a variadic function that finds the maximum value among the provided arguments.
  3. Write a function that takes a string and returns the string itself along with its length.
  4. Implement a function that calculates the area of a circle (given the radius) and returns both the area and an error if the radius is negative.

Next Steps

Tomorrow, we’ll wrap up our first week with a Review and Practice session to solidify everything you’ve learned!

Complete the quiz to wrap up Day 6!

Quiz

Complete this quiz with a minimum score of 80% to mark Day 6 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!