Skip to the content.

Using functions

In this chapter, we will discuss how you can define and use functions. Functions are great when you have the same type of code used in many places. By using functions, you thereby reduce repetition.

Introduction

This chapter will cover:

Why functions

As soon as you have a set of statements you repeat in many places, it’s a good use case for creating a function. Typical things you put in functions are logging to a file, performing a calculation or talking to a data source.

Your first function main()

So far, you’ve seen the function main(), define like so:

func main(){
}

There’s only one such function, it’s called an entry point and represents the start of the program. You can however define other functions.

The anatomy of a function

A function consists of various parts. By incorporating all these parts, you ensure you have a reusable piece of code you can use in many places.

Here are the parts you need to care about:

Here’s an example:

func add(first int, second int) int {
  return first + second
}

In the preceding code, the function is named add(). It has the parameters first and second. The function body, what the function does, consists of this code:

return first + second

Adding a return type

To add a return type, we add that after the function parenthesis in form of a type. Here’s an example:

add(firstNumber int, secondNumber int) int {
 ...
}

Because we’ve added a return type of int, our function must return something. A way to return a value is by using the keyword return, like so:

add(firstNumber int, secondNumber int) int {
  return firstNumber + secondNumber 
}

Named return

We can also name the return parameter like so:

add(firstNumber int, secondNumber int) (sum int) {
  sum = firstNumber + secondNumber 
  return
}

Multiple returns

It’s possible to return more than one value.

Just like you returned a named parameter via (sum int), you can comma separate like so (sum int, product int). When returning multiple values, you can type like so:

sum = first + second
product = first * second
return

Both sum and product are assigned values and you have a closing return.

Putting it altogether you get a function that looks like so:

func calc(first int, second int) (sum int, product int) {
  sum = first + second
  product = first * second
  return
}

To call the function, you type like so:

sum, product := calc(1, 2)
fmt.Println(sum)
fmt.Println(product)

Note how you assign the two returned values to variables sum and product.

Assignment - adding a function to a program

  1. Create a file main.go and give it the following content:

     package main
        
     import "fmt"
        
     func main() {
        
     }
    
  2. Add a function log(), that we can use to print messages.

    Added to the program, your code should now look like so:

     package main
        
     import "fmt"
        
     func log() {
       fmt.Println("message")
     }
        
     func main() {
       log()
     }
    

At this point, the log() function isn’t very flexible, it prints “message” every time it’s invoked.

To make the log() function more flexible, lets add a parameter.

Adding a parameter

A parameter needs a data type, in this case, we will make it of type string.

  1. Add the parameter within the parenthesis (), like so:

     func log(message string) {
       fmt.Println(message)
     }
        
     // to use
     log("hi")
     log("there")
    

Note how the log() function takes the parameter message that is of type string. Our code is more flexible.

Solution

package main
    
import "fmt"

func log(message string) {
  fmt.Println(message)
}

func main() {
  log("hi")
  log("there")
}