Functions in Go (challenge answer)
Tags: beginners, tutorial, learning, go
Published: 2019-07-15 11:08:42

---
title: Functions in Go (challenge answer)
published: true
tags: beginners, tutorial, learning, go
canonical_url: https://www.codetips.co.uk/languages/go/functions-in-go-challenge-answer/
---

 ![Functions in Go (challenge answer)](https://images.unsplash.com/photo-1516589091380-5d8e87df6999?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

Warning: **Challenge spoiler alert**

At the end of the last article ( [“Functions in Go”](https://www.codetips.co.uk/functions-in-go/) ) we asked you to update the `printAgeBracket` function to accept a `Boolean` parameter, so we could conditionally print a statement and reduce duplication.

If you haven't attempted it; we're about to give you the answer, so now's a good time to go and try it.

For reference, here was the `printAgeBracket` statement we asked you to amend:

<!--kg-card-begin: code-->
```
func printAgeBracket(name string, age int) {
	log.Printf("%s: ", name)

	if age < 13 {
		log.Println("I am considered a child")
	} else if age < 20 {
		log.Println("I am considered a teenager")
	} else if age < 70 {
		log.Println("I am considered an adult")
	} else {
		log.Println("I am considered a pensioner")
	}
}
```
<!--kg-card-end: code-->

And this was the code we wanted to incorporate into this function:

<!--kg-card-begin: code-->
```
log.Printf("Hello World. My name is %s.", myName)

if myAge < 13 {
	log.Println("I am considered a child")
} else if myAge < 20 {
	log.Println("I am considered a teenager")
} else if myAge < 70 {
	log.Println("I am considered an adult")
} else {
	log.Println("I am considered a pensioner")
}
```
<!--kg-card-end: code-->

The only thing stopping us from using the `printAgeBracket` function is we print out a different statement; `Hello world. My name is: %s` vs `$s: `.

One possible solution to this problem is to add a new parameter to `printAgeBracket`, called `isFriend`, and add an `if/else` statement to our function:

<!--kg-card-begin: code-->
```
func printAgeBracket(name string, age int, isFriend bool) {
	if isFriend {
		log.Printf("%s: ", name)
	} else {
		log.Printf("Hello World. My name is %s.", name)
	}

	if age < 13 {
		log.Println("I am considered a child")
	} else if age < 20 {
		log.Println("I am considered a teenager")
	} else if age < 70 {
		log.Println("I am considered an adult")
	} else {
		log.Println("I am considered a pensioner")
	}
}
```
<!--kg-card-end: code-->

We then need to amend the lines that use this function to use this new parameter:

<!--kg-card-begin: code-->
```
printAgeBracket(friendOneName, friendOneAge, true)
printAgeBracket(friendTwoName, friendTwoAge, true)
printAgeBracket(friendThreeName, friendThreeAge, true)
printAgeBracket(friendFourName, friendFourAge, true)
printAgeBracket(friendFiveName, friendFiveAge, true)
```
<!--kg-card-end: code-->

If our new `isFriend` parameter is `true`, we print out the first statement, `else` we print out the second statement.

If you didn’t get this far on your own, take five minutes and see if you can amend the following code to use the `printAgeBracket` function:

<!--kg-card-begin: code-->
```
log.Printf("Hello World. My name is %s.", myName)

if myAge < 13 {
	log.Println("I am considered a child")
} else if myAge < 20 {
	log.Println("I am considered a teenager")
} else if myAge < 70 {
	log.Println("I am considered an adult")
} else {
	log.Println("I am considered a pensioner")
}
```
<!--kg-card-end: code-->

Done? Excellent!

You should have ended up with the following:

<!--kg-card-begin: code-->
```
printAgeBracket(myName, myAge, false)
```
<!--kg-card-end: code-->

The `isFriend` parameter is `false` so it prints out the statement in the `else` statement, and we’ve just removed more duplication from our code.

Our whole programme now looks like this:

<!--kg-card-begin: code-->
```
package main

import "log"

func printAgeBracket(name string, age int, isFriend bool) {
	if isFriend {
		log.Printf("%s: ", name)
	} else {
		log.Printf("Hello World. My name is %s.", name)
	}

	if age < 13 {
		log.Println("I am considered a child")
	} else if age < 20 {
		log.Println("I am considered a teenager")
	} else if age < 70 {
		log.Println("I am considered an adult")
	} else {
		log.Println("I am considered a pensioner")
	}
}

func main() {

	// Start of our code

	myName := "Simon"
	myAge := 29

	friendOneName := "David"
	friendOneAge := 17

	friendTwoName := "Bill"
	friendTwoAge := 42

	friendThreeName := "Charlie"
	friendThreeAge := 12

	friendFourName := "Abby"
	friendFourAge := 24

	friendFiveName := "Edith"
	friendFiveAge := 74

	printAgeBracket(myName, myAge, false)

	log.Println("I have five friends, and they are as follows:")

	printAgeBracket(friendOneName, friendOneAge, true)
	printAgeBracket(friendTwoName, friendTwoAge, true)
	printAgeBracket(friendThreeName, friendThreeAge, true)
	printAgeBracket(friendFourName, friendFourAge, true)
	printAgeBracket(friendFiveName, friendFiveAge, true)

	// End of our code

}
```
<!--kg-card-end: code-->

Our new function has reduced a lot of duplication in our code, and it makes maintaining our programme easy (e.g. adding a new age bracket), but there’s still a couple of things that we should improve:

1. Every time we create a new friend, we have to create two new variables. We’ve currently got fifteen lines of code just to create five friends.
2. When we print how many friends we have ( “I have five friends, and they are as follows" ), we have to add the number (five) manually. We have a real risk that we could add/remove a friend(s) and forget to update this.

Both of these things have reduced our maintainability. Stay tuned for future articles, where we’ll fix these problems.

If you need any help understanding what we’ve discussed in this article, [reach out to us](https://www.codetips.co.uk/contact-us/) and we’ll do what we can to help.

---

[CodeTips](https://www.codetips.co.uk) strives to help beginners, with zero or very little experience, learn to code.

We do cross-post to other sites to reach a wider audience, but why not [subscribe to our newsletter](https://mailchi.mp/92cccf7024b2/codetips) and get the newest articles straight to your mailbox? 

The original source for this content is [CodeTips](https://www.codetips.co.uk). The original content is kept up-to-date, but other sources may not be the latest version.