Course / The terminal and the language
Double it
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitGoal
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// TODOcomment and areturnline you will change. - Use the parameter
n. Do not hard-code answers:Doublemust work for any number it is given. - Negative numbers double to more negative numbers, and
0doubles to0. - 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
- The function receives one number in the parameter
n. Whatever youreturnis what the caller gets back. - Doubling means multiplying by 2, or adding the number to itself.
return n * 2, replacing thereturn 0line.