Start learning free

LESSON 27 OF 338 · FUNCTIONS I

Functions in Go: func, Calling and Exported Names

Lesson Read the full explanation

A function is a container for a few lines of code that perform a specific task.

Functions allow us to organize code by breaking it into smaller, more manageable chunks that each do one clear thing.

Here's how a function definition looks in Go:

func functionName() {
    // function body
}

There are two main components of a function:

1. The function header:

  • func tells Go that we are defining a function.
  • functionName is the function's name.
  • Parentheses (()) appear after the name. We'll use them for parameters in later lessons.
  • Curly braces ({}) contain the function body.

2. The function body:

This is the code that Go executes whenever the function is called.

Here's an example:

func sayHello() {
    fmt.Println("Hello World")
}

Calling a function

A function declaration alone does not run the code inside the function body. It only declares that the function exists.

The code inside a function runs only when the function is called.

You can call or invoke a function by typing its name followed by parentheses:

func sayHello() {
    fmt.Println("Hello World")
}

func mainFunction() {
    sayHello() // Output: Hello World
}

All of the code inside the function body is executed every time the function is called.


Capitalization = exported:

In Go, a name that starts with a Capital letter (Foo) is exported — other packages can use it. A name that starts with a lowercase letter (foo) is private to its own package. This rule applies to functions, types, and struct fields alike.

func SayHello() {} // exported (capital S) — usable from other packages
func sayHello() {} // private to this package (lowercase s)

That's why you'll see fmt.Println with a capital P in the coding tutorials: it's exported from the fmt package.


Assignment
Follow the Coding Tutorial and let's write some functions.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Functions in Go, explained

    What a function is

    A function is a named block of code that does one job. You write it once and run it whenever you need it. Splitting a program into functions keeps each piece small enough to read, test and change on its own.

    Every Go function has a header and a body:

    func functionName() {
        // function body
    }
    
    • func is the keyword that starts a function declaration.
    • functionName is the name you will use to call it.
    • The parentheses hold the parameters. This function takes none, so they are empty.
    • The curly braces wrap the body, the statements that run each time the function is called.

    A function with nothing written after the parentheses returns no value. It just does its work, such as printing.

    Defining is not calling

    A declaration only tells Go that the function exists. Its body does not run until the function is called, which means writing its name followed by parentheses.

    func sayHello() {
        fmt.Println("Hello World")
    }
    
    func mainFunction() {
        sayHello() // Hello World
        sayHello() // Hello World
    }
    

    Each call runs the whole body from top to bottom, so two calls print the greeting twice. The parentheses matter: sayHello on its own is just the function as a value, and Go refuses to compile a statement that does nothing with it. In a test program I wrote, a bare sayHello line gave the error sayHello (value of type func()) is not used.

    Order in the file doesn't matter either. Go lets a function call another one that is declared further down, because package-level declarations are all visible to each other. In a full program, the call has to start somewhere: execution begins in a function named main, and the lesson's mainFunction plays that role in the editor.

    Capital letters decide who can use a name

    Go has no public or private keywords. The first letter of a name does the job:

    func SayHello() {} // exported (capital S): usable from other packages
    func sayHello() {} // private to this package (lowercase s)
    

    A name that starts with a capital letter is exported, so code in other packages can use it. A lowercase name is visible only inside its own package. The rule covers functions, types and struct fields alike.

    That is why you write fmt.Println with a capital P: Println is exported from the fmt package. Writing fmt.println doesn't compile, because that name is not exported.

    The exercise, step by step

    The lesson asks for a function that greets everyone, then a call to it from mainFunction.

    1. Define sayHello above mainFunction. Use func, the name, empty parentheses and a pair of braces. The reasoning is reuse: the greeting lives in its own block.
    2. Print inside the braces. fmt.Println("Hello everybody!") is the body. It runs only when the function is called.
    3. Call it from mainFunction. Without the call, defining the function changes nothing about what the program prints.

    The finished code:

    func sayHello() {
        fmt.Println("Hello everybody!")
    }
    
    func mainFunction() {
        sayHello()
    }
    

    Common mistakes

    • Forgetting the parentheses on a call. sayHello without () does not call anything.
    • Putting the opening brace on its own line. Go inserts a semicolon at the end of the header line, so func sayHello() followed by a line holding only { fails with unexpected semicolon or newline before {. The brace goes on the header line.
    • Defining but never calling. A function nobody calls prints nothing.
    • Lowercase fmt.println. The printing function is Println.
    • Calling a function with the wrong capitalization. Names are case-sensitive, so SayHello and sayHello are two different names.

    Key takeaways

    • A function is declared with func, a name, parentheses and a brace-wrapped body.
    • The body runs once per call, and a call is the name followed by parentheses.
    • Declaration order inside a package does not matter, but statements inside a body run top to bottom.
    • Capitalized names are exported from their package, and lowercase names are private to it.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free