Thuta Learning
IntermediateProgrammingbeginner

Functions

Relax. We'll talk through this in plain words — no textbook voice.

A function is a named, reusable block of code for doing one job. In Go, you write the input parameters and return type explicitly, which makes it easy to see what a function does at a glance.

go
package main

import "fmt"

func add(x int, y int) int {
    return x + y
}

func divide(a int, b int) (int, bool) {
    if b == 0 {
        return 0, false
    }
    return a / b, true
}

func main() {
    fmt.Println(add(42, 13))

    result, ok := divide(10, 2)
    fmt.Println(result, ok)
}

add function takes two integers and returns one integer. divide returns both a result and a success status at once.

You should see
55 5 true

Info

Because Go functions can return multiple values, writing a result-plus-error pattern feels natural.

Easy traps

  • If you declare a return type but the function doesn't actually return a value, you'll get a compile error.
Functions | Thuta Learning