Lesson 12. Functions
After reading lesson 12, you’ll be able to
- Identify the parts of a function declaration
- Write reusable functions to build up larger programs
This lesson begins by examining the standard library documentation for functions that were used in earlier lessons.
Once you’re familiar with the syntax for declaring functions, you’ll write functions for a weather station program. The Rover Environmental Monitoring Station (REMS) gathers weather data on the surface of Mars. You’ll write functions that could conceivably be part of a REMS program, such as converting temperatures.
Consider this
Make a sandwich. It sounds simple, but many steps are involved. Wash the lettuce, slice a tomato, and so on. Maybe you go so far as to harvest the grain, grind it into flour, and bake the bread, or maybe those functions are provided by a farmer and a baker.
Break down the process with a function for each step. Then later, if you need tomato slices for a pizza, that function can be reused.
What is something else from your daily life that you can break down into functions?
The Go package documentation at golang.org/pkg lists the functions that are declared in every package of the standard library. There are a lot of handy functions—more than this book can possibly cover.