Functions are great for code reuse, this means that when you find yourself repeating the same pattern over and over, it might be a good idea to try and *abstract* it into a function. When you abstract your pattern into a function, this means we take the logic it takes to solve our problem and generalize it into a function to solve multiple problems. Let’s say you need to square numbers:

fmt.Println(5 * 5) fmt.Println(6 * 6) // ... fmt.Println(100 * 100)

This can quickly get out of hand! Writing all of this down does not align with your programming chops since you can write a function to handle this problem!

func squareNum(num int) { fmt.Println(num * num) }

`squareNum()`

abstracts the logic for squaring a number into one single place! If you decide something about your formula is wrong, then you only have to change that code in a single place!

### Instructions

**1.**

Our sophisticated mathematical program is developing some weird data, and we think it’s because some typos crept in while we were copying down our formula. This series of operations seems to be very important to us, so let’s write a function that does it instead of calling it multiple times.

Write a function called `specialComputation()`

which takes a parameter `x`

, a `float64`

, and returns a `float64`

.

**2.**

Inside the body of `specialComputation()`

return the value computed by the following line of code:

math.Log2(math.Sqrt(math.Tanh(x)))

**3.**

Now update the reassignments in `main()`

to use `specialComputation()`

, our function, instead of making each call to `math.Log2(math.Sqrt(math.Tanh()))`

separately.

Apply and reassign `specialComputation()`

to each of `a`

, `b`

, `c`

, and `d`

. Remove the earlier computations which had some typos in them.

**4.**

Well our science branch just got back to us explaining that we don’t actually need `Tanh()`

, we actually just need `Tan()`

. Inside `specialComputation()`

change the part using `math.Tanh()`

to use `math.Tan()`

instead.

# Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.