Check out a free preview of the full Go for JavaScript Developers course:
The "Functions Exercise" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to practice writing functions by creating an average function, and then refactoring the function to be variadic.

Get Unlimited Access Now

Transcript from the "Functions Exercise" Lesson

>> Our next exercise is going to look a little bit at functions. And have some practice around both regular functions and variadic functions. So we're gonna tackle both Exercise #4A and #4B at the same time. So if we hop over to the complex structures directory and look at the exercise #4A file.

[00:00:22] The goal of this exercise is to practice writing some functions. We'll create a file called exercise_4a.go, you can throw that in the code directory. The first thing you're going to do is create a function called average, which is going to take three arguments. And for this example, let's try to do it the explicit way, where you're actually passing three exact arguments.

[00:00:39] And all of those arguments should be floats. At the end of the day, the function should return at the average of those three arguments. Which is also gonna be a float, and then make sure to call that function a main. And see something print to the console. In #4B, we're gonna take that exact function and refactor it to use a variadic version of it.

[00:00:59] So you can either refactor the code that you write from exercise #4A or start from scratch. If you just really wanna practice setting up a go template file. And then for the directions just refactor your code using the spread operator type syntax. Well, we're still calculating that average, so a couple hints to get you going.

[00:01:16] The first one is that to find the length of a collection, you can use the built in function LEN. An LEN is gonna take whatever collection you pass it, and give you back the length of that thing. And then, another hint is keep an eye on what type LEN some collection gives you back.