Course outline
Course / The terminal and the language

Double it

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/12-01-double-it
cd 12-01-double-it
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

Fill in Double so it takes one whole number and returns that number doubled.

Example

fmt.Println(Double(4))  // 8
fmt.Println(Double(0))  // 0
fmt.Println(Double(-3)) // -6

A function is like a vending machine: the number you pass in is the parameter, and the value you return is what comes out.

Rules

  • Edit double.go. The function has a // TODO comment and a return line you will change.
  • Use the parameter n. Do not hard-code answers: Double must work for any number it is given.
  • Negative numbers double to more negative numbers, and 0 doubles to 0.
  • Do not change the line func Double(n int) int {. Do not add imports.

What you'll practice

Declaring a parameter with a type, using it inside the function, and returning a value of the declared type.

Run it

gonomicon fetch week-1/12-01-double-it
cd 12-01-double-it
gonomicon test
gonomicon submit

Hints

  1. The function receives one number in the parameter n. Whatever you return is what the caller gets back.
  2. Doubling means multiplying by 2, or adding the number to itself.
  3. return n * 2, replacing the return 0 line.

Something wrong with this lesson? Report a problem