×

go · level 1 · unit 7 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a recipe card with a name on top.

A function is a set of instructions wrapped up under one name. Write the recipe once, then cook it as often as you like, without copying a line. You have been writing one all along: main!

To a function, write func, a name, empty brackets and a block: func cheer() {. Defining it does not run it. It only writes the recipe card.

To it, write its name with brackets, like cheer(). Every line inside then runs, top to bottom. Go reads the whole file first, so a function can sit above or below main.

package main
import "fmt"
func cheer() {
	fmt.Println("Go team!")
	fmt.Println("You can do it!")
}
func main() {
	cheer()
	cheer()
}

Go starts every program by calling main for you, while your other functions wait to be called.