Video Coming Soon...

Created by Zed A. Shaw Updated 2025-10-07 14:32:54

11: Basic Syntax: Functions

WARNING This exercise is in DRAFT status, so there may be errors. If you find any, please email me at help@learncodethehardway.com so I can fix them.

You are now ready to learn another fundamental building block of programming: the function. A function is:

  1. A block of code...
  2. With a name (or sometimes anonymous)...
  3. And variables to configure it...
  4. That can return new variables to use.

You've been using functions this whole time when you print text to the screen:

fmt.Println("See? Like this.")

In this exercise you'll learn how to make your own functions so you can keep your code organized and easy to understand.

How to Make a Function

You need 6 things to create a function:

  1. The keyword func
  2. A name like PrintStuffNow or print_stuff_now
  3. A set of parenthesis ( and )
  4. Possible variables inside the () and their types.
  5. A block of code to run inside { and }
  6. A possible return with a value or variable to give something back to the caller of this function.

Here's an example of all 6 things:

func PrintName(name string) string {
    fmt.Println("Your name is", name)
    return "done"
}

Let me break down every part of that first line so you know what it does:

You don't need all of the features at once, only use what's needed. For example, this is also a function:

func something() {
    fmt.Println("something")
}

There's even situations when you need to use a function without a name (called an anonymous function):

func () { fmt.Println("John Cena") }

These come in to play later when you pass functions to other functions, but that's an advanced topic for now.

How to Use Your Functions

You've already been using functions when you use fmt.Println("hi") but you haven't used functions that return values yet. First, you use a function by "calling" it, and that's done by putting () (parenthesis) after its name:

PrintName("Zed")

You also have to include any parameters (that's the variables listed inside the ()) the function needs.

The PrintName function above though also returns a value, and to receive that you only need to assign it to a variable like you would with a number or a string:

is_done := PrintName("Zed")
fmt.Println(is_done)

This would print out "done" if you ran it because the PrintName function uses return "done" to give a variable/value back to the caller. The "caller" is any place in your code that calls another function.

How a Function Works

A function acts as a block of code that your main code jumps to temporarily. While the function's code is executing it will use its own set of variables so they don't impact your main code's variables. This is very important because you want your functions to run no matter where they are used, and if they changed other code you couldn't do that. Here's a function that does mostly nothing to demonstrate:

func DoNothing() {
    a := 1
}

func main() {
    a := 100
    DoNothing()
    fmt.Println(a)
}

This is what happens when this code runs:

  1. Your main() function is the entry point, so you program starts there.
  2. main() first create a variable name a with a value of 100.
  3. main() then calls DoNothing(), which makes main() the "caller".
  4. Go then jumps to the line where fund DoNothing() is defined so it can run that code.
  5. Inside DoNothing() it sets the variable a to 1, however this is a totally different variable named a that does NOT change the one in main().
  6. When the { at the end of DoNothing() is reached Go jump back to the main() right after its call to DoNothing().
  7. At that point the fmt.Println(a) line runs, which will print out 100 and NOT 1.

Functions, Variables, and return

A final piece of the function puzzle is that you can place a variable anywhere you place a function with a return and vice-versa. For example, if you have this code:

x := Add(1, 2)
y := Sub(x, 3)

Then you could also write:

y := Sub(Add(1, 2), 2)

It's not a very nice thing to write though as the second form is difficult to understand, but it does come up in some functions you use. I use this in the code for this exercise so you have some experience translating the two styles.

The Code

Here's an example that uses functions to do some basic math.

View Source file ex11/main.go Only

package main

import (
    "fmt"
)

func Add(a float64, b float64) float64 {
    return a + b
}

func Sub(a float64, b float64) float64 {
    return a - b
}

func Mul(a float64, b float64) float64 {
    return a * b
}

func Div(a float64, b float64) float64 {
    return a / b
}

func main() {
    result := Add(Sub(3,4), Mul(10, Div(200, 10)))

    fmt.Println("The result is:", result)
}

As usual, type it in gradually and get it to work slowly. You might need to enter each function before creating the main() since main() needs the other functions.

The Breakdown

NOTE It's important to know that you could name these functions anything, and as long as you kept the functions' blocks the same they would work the same. If you don't believe me name them after fruit and watch how they work the same. It's very important you understand this as later you'll run into weird programmers like me who name their functions something strange for fun.

The Practice

  1. Break It -- There are many ways to break this one. Try calling functions without parameters, calling the wrong functions, misspelling the function names, using the wrong variables inside the functions, and more.
  2. Change It -- The most important thing you can do is convert this heinous string of nested functions into separate variables as mentioned previously.
  3. Remake It -- As usual, write a description the recreate it from memory using the description. The more you do this, the better you'll get.

Study Drills

  1. Use these functions to do even more math.
  2. Write more functions that do more math. Maybe a modulus function?
  3. Write some more functions that take int64 instead. How do they compare to the float64 versions.
Previous Lesson Next Lesson

Register for Learn Go the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.