Functions

Question Click to View Answer

What does the following code print?

package main

import "fmt"

func funify(word string) string {
    return word + " is really fun!"
}

func main() {
    fmt.Println(funify("cheese"))
}

cheese is really fun! is printed.

The funify() function takes a string as an argument and returns a string.

funify() appends the "is really fun" string to the argument that's invoked with the function.

What does the following code print?

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

func main() {
    fmt.Println(add(3, 5))
}

8 is printed.

The add() function adds two integers.

What does the following code print?

func average(nums []float64) float64 {
    total := 0.0
    for _, v := range nums {
        total += v
    }
    return total / float64(len(nums))
}

func main() {
    scores := []float64{10, 11, 12, 13}
    fmt.Println(average(scores))
}

11.5 is printed.

The average() function loops through a slice of floats and calculates the sum of all the numbers. The function returns the sum divided by the total number of elements in the slice, or the average.

The float64() function is used to convert an integer into a floating point number. You can only divide a floating point number by another floating point number.

What does the following code print?

func product(a int, b int) (res int) {
    res = a * b
    return
}

func main() {
    fmt.Println(product(5, 3))
}

15 is printed.

The product() function has a named return type.

A return statement without arguments returns the named return values. This is known as a "naked" return.

Naked returns should only be used in short functions.

What does the following code print?

func weird() (string, bool) {
    return "casper", true
}

func main() {
    if str, ok := weird(); ok {
        fmt.Println(str, "is a ghost")
    }
}

The weird() function returns a string and a boolean value.

str, ok := weird() assigns str to the string value that's returned by weird() and ok to the boolean value that's returned.

Go functions often return multiple values.

What does the following code print?

func addAll(nums ...int) int {
    total := 0
    for _, v := range nums {
        total += v
    }
    return total
}

func main() {
    fmt.Println(addAll(1, 2, 3, 4))
}

10 is printed.

addAll is an example of a variadic function. Variadic functions take an artibrary number of arguments.

Both addAll(1, 2) and addAll(1, 2, 3, 40, 10) will also work.

What does the following code print?

func addAll(nums ...int) int {
    total := 0
    for _, v := range nums {
        total += v
    }
    return total
}

func main() {
    points := []int{5, 10, 15}
    fmt.Println(addAll(points...))
}

30 is printed.

Variadic functions can be invoked with a slice of arguments as well.

What does the following code print?

func plusSeven(num int) int {
    plusFive := func(num int) int {
        return num + 5
    }
    return plusFive(num) + 2
}

func main() {
    fmt.Println(plusSeven(20))
}

27 is printed.

The plusSeven() function uses a closure to define an inner function. The plusFive variable is assigned to the anonymous function that's the inner function.

What does the following code print?

func factorial(x uint) uint {
    if x == 0 {
        return 1
    }
    return x * factorial(x-1)
}

func main() {
    fmt.Println(factorial(5))
}

120 is printed.

factorial() is an example of a recursive function.

Recursive functions are invoked in the function definition itself.