go · level 1 · unit 7 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a slot where your function catches information.
Picture your function as a machine. Right now it does the same thing every time. Cut a slot in the front, and you can feed in something new on each go.
That slot is a parameter. It sits in the brackets of the func line, with its type after it: func greet(name string). Inside the function, use it like any other box.
The value you drop in when you call is the . greet("Mia") puts "Mia" into name. Hand over the wrong type, like greet(7), and Go refuses to run.
package main
import "fmt"
func greet(name string) {
fmt.Printf("Hello, %v!\n", name)
}
func main() {
greet("Mia")
greet("Leo")
} For two slots, separate them with a comma: func order(food string, drink string).
guess first
Two slots this time. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
package main
import "fmt"
func order(food string, drink string) {
fmt.Printf("I would like %v with %v.\n", food, drink)
}
func main() {
order("lemonade", "chips")
}
your turn
Your turn. Run the birthday card machine.
Type a name between each pair of quotes and a whole-number age after each comma. Then run it and watch one machine make two cards.
output
press run to see what happens
fix it
Rex is hungry, but Go will not run this. Fix it!
output
press run to see what happens
- Read the error. Which argument does Go say is the wrong type?
- The scoops parameter is an
int, but "two" is a string. - Change "two" to 2 with no quotes, then run.
nailed it
your functions take orders now. :D
- A parameter is a slot in the brackets of the
funcline. - Each parameter has a type, like
name string. - Arguments fill parameters in order: first into first.
new words in your collection
- parameter
- argument
This lesson isn’t saved. A free account remembers every lesson you finish.