This course has been updated! We now recommend you take the Basics of Go course.
Transcript from the "Functions Solution" Lesson
[00:00:00]
>> Okay, let's work through these solutions to 4a and 4b. So for 4a, I'm going to create an exercise 4a file in the code directory of Section four. I will go ahead and set up the template. The first task is to create a function called average that takes three explicit arguments, each of which are floats.
[00:00:42] So above main, I will create, Func average. It's gonna take three numbers, so I'm gonna call them num1, num2, and num3, all of which are integers. And you'll notice that the pattern I've been using is that if all three of the arguments have the same type, I can add that type to the end of the internal argument parentheses.
[00:01:09] I don't have to explicitly say that each of these are ints. So I'm gonna take those out and just leave that int at the end of the file. And as I'm saying that out loud, I realize that they are all supposed to be floats. So we're gonna go ahead and change that to float64 type instead.
[00:01:30] And it also said that the function should return the average of the three arguments, which is also a float. Which means that our function signature needs to indicate that we are returning a float64 before we execute any code internally. So for this guy, the approach I'm gonna take is I'm gonna set a variable called total and set that equal to the three arguments that I have here, num1 + num2 + num3.
[00:02:01] And then I wanna return that total divided by 3 to get the average, Of our numbers. And then in main we just need to remember to call that function and pass it a few numbers. So let's do like, 10, 5, 7. And then I will wrap this in a Println, just so we can see what we're getting back.
[00:02:30] Save the file and then in my terminal, I'll go run a section 4 code exercise_4a.go, and I've got 7.333333. So that completes 4a, let's switch over to 4b. So in 4b, we're gonna re-factor the code to use a variadic, that takes an unknown number of arguments. So I'm just gonna stay in my exercise 4a.go file.
[00:03:05] I'm going to copy this and then comment it out. So when the solution gets pushed up later, you have both examples if you want them. But instead of explicitly passing in three numbers, I'm going to change this to say numbers and then indicate that there is an unknown set of float64 is coming into this function.
[00:03:25] I still wanna return that same average, which is gonna be a float64. But instead of being able to explicitly add together the three numbers, We are going to have to do some stuff differently. So for this guy, we're gonna circle back to our for loop with the range keyword.
[00:03:45] So far I know I'm not gonna be using the index, I'll give it that underscore each individual number. Assign that to a value of our range keyword and the collection we're iterating over which is numbers, or whatever we wanna do inside that. And then in here, I know I wanna take each of those numbers and add it to some sort of total.
[00:04:05] And so up here, I'm going to set a initial value of my total to 0. And then within my function here, I want to add each number to that total variable. Once my collection has been iterated over, I'm going to return total divided by the length of the numbers collection that we're starting with.
[00:04:35] If we run the code as is, we're gonna run into a few errors. And the first one we see says, invalid operation total + equal number, mismatch types int and float. So we're looking at line 15 of our code here. On line 15, we're trying to add the variable total, which is an int with the variable number.
[00:04:57] And if number, we have a variadic function here, it's telling me that every number that I'm working with here is a float64. But total is defined on line 13. And total is defined as an integer. Because we're doing the mathematics and we're in go, we need to make sure that both of the size of the mathematical equation are of the same type.
[00:05:14] So to fix that, I can simply change this to a float. So go has an equal type on both sides. So I'll save that and I'll rerun the program. It's now we're seeing a second error. Invalid operation total divided by len numbers mismatch types float64 and int, and now we're looking at our go code line 17.
[00:05:36] So on line 17 of our code, you see that I'm dividing the total, which is a float with whatever is returned from len of numbers. And so the len function in go is gonna return an integer. So now we're once again trying to divide a float because we modified this to be a float on line 13 with len numbers, which is an integer.
[00:05:55] So to fix this, we can simply use our type conversion and change len numbers to be a float64. So if I run this code, we now get back to our 7.33333 result, which is the same result we had when we had implicitly passed in our num1, num2, and num3.