100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Functions in Go

Learn how to declare, call, and use functions as first-class values in Go.

FunctionsBeginner7 min readJul 8, 2026
Analogies

Introduction

A function is a reusable block of code that performs a specific task. In Go, functions are declared with the func keyword and are central to organizing programs into small, testable pieces. Go also treats functions as first-class values, meaning they can be assigned to variables, passed as arguments, and returned from other functions.

🏏

Cricket analogy: A death-over specialist like Jasprit Bumrah is a reusable weapon called into any match situation; Go functions, declared with func, are similarly reusable blocks you can call repeatedly, and even pass around like naming a bowler as a variable for the next over.

Syntax

go
func name(param1 type1, param2 type2) returnType {
    // function body
    return value
}

Explanation

The func keyword starts the declaration, followed by the function name, a parenthesized parameter list where each parameter has an explicit type, and an optional return type. If two or more consecutive parameters share the same type, you can omit the type from all but the last one, e.g. func add(a, b int) int. A function with no return value simply omits the return type.

🏏

Cricket analogy: Declaring a bowler's spell like func bowl(overs, speed int) wickets names the action, lists typed inputs, and states what comes back, just as Go's func syntax lists typed parameters before an optional return type.

Example

go
package main

import "fmt"

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

func greet(name string) {
    fmt.Println("Hello,", name)
}

func main() {
    sum := add(3, 4)
    fmt.Println("Sum:", sum)
    greet("Gopher")

    var op func(int, int) int = add
    fmt.Println("Via variable:", op(10, 5))
}

// Output:
// Sum: 7
// Hello, Gopher
// Via variable: 15

Key Takeaways

  • Functions are declared with func, a name, typed parameters, and an optional return type.
  • Consecutive parameters of the same type can share a single type declaration.
  • Functions are first-class values: assignable to variables, passable as arguments, and returnable from other functions.
  • A function with no return type performs an action without producing a value.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#FunctionsInGo#Functions#Syntax#Explanation#Example#StudyNotes#SkillVeris