Chapter 3 - Functions

Exercise 1: fmt.Printf

The program below should produce a table with student names, their scores on three tests, and the average of those scores.

Write a scoreSummary function that accepts a string parameter with the student name, followed by three float64 parameters with the test scores. Calculate the average of the scores by adding them all together and dividing the result by three. Then call fmt.Printf to print a table row with the following columns:

  • The student name string, with a width of 10 characters.
  • The first test score, with a with of 8 characters, 2 of them decimal places.
  • The second test score, with a with of 8 characters, 2 of them decimal places.
  • The third test score, with a with of 8 characters, 2 of them decimal places.
  • The average of the scores, with a with of 8 characters, 2 of them decimal places.

Separate the columns with a vertical bar surrounded by spaces, just like you see in the call to Printf in the main function.

The program output should look like this:

      Name |  Score 1 |  Score 2 |  Score 3 |  Average
------------------------------------------------------
  Jermaine |    95.40 |    82.30 |    74.60 |    84.10
    Jessie |    79.30 |    99.10 |    82.50 |    86.97
     Lamar |    82.20 |    95.40 |    77.60 |    85.07
package main

import "fmt"

// YOUR CODE HERE: Write a scoreSummary function that will
// work with the calls below.

func main() {
	fmt.Printf("%10s | %8s | %8s | %8s | %8s\n",
		"Name", "Score 1", "Score 2", "Score 3", "Average")
	fmt.Println("------------------------------------------------------")
	scoreSummary("Jermaine", 95.4, 82.3, 74.6)
	scoreSummary("Jessie", 79.3, 99.1, 82.5)
	scoreSummary("Lamar", 82.2, 95.4, 77.6)
}

When you’re ready, have a look at our solution.

Exercise 2: Return Values

You’re fed up with the neighborhood wildlife destroying your gardens. You have three rectangular garden plots that you need to surround with fencing:

  • An 8.2 by 10 meter plot
  • A 5 by 5.2 meter plot
  • A 6.2 by 4.5 meter plot

You need to figure out how much fencing to buy. You’ll need to calculate the perimeter (two times the length plus two times the width) for each of these plots, then add the three perimeters together to get the total length of fencing.

Write a perimeter function that takes two float64 parameters representing the length and width. perimeter should return a float64 value representing the perimeter length.

Then, in your main function, call perimeter three times to calculate perimeters for the plots of land above. Add the three perimeters together, and print the total.

package main

import "fmt"

// YOUR CODE HERE: Write a "perimeter" function.

func main() {
	// YOUR CODE HERE: Call "perimeter" three times.
	// Add the three return values together, and store the
	// total in the "total" variable.
	fmt.Println("You'll need", total, "meters of fencing")
}

Output:

You'll need 78.6 meters of fencing

When you’re ready, have a look at our solution.

Exercise 3: Error Handling

Update your perimeter function from the previous exercise to return an error if either the length or width are negative.

  • If the given length is negative, return a perimeter of 0 and an error with the message “a length of [length] is invalid”.
  • If the given width is negative, return a perimeter of 0 and an error with the message “a width of [width] is invalid”.
  • Otherwise, return the calculated perimeter, and an error value of nil.

In your main function, test returning an error by calling perimeter with a negative value. If the returned error value is not nil, print the error message and exit the program. Otherwise, print the returned perimeter.

Sample output:

2019/05/25 15:06:35 a width of -10.00 is invalid
exit status 1

Here’s our solution.