Check out a free preview of the full Go for JavaScript Developers course:
The "Functions & Variadic Functions" 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:

Brenna demonstrates how to build functions containing for loops, how to name returned variables within the function signature, and how to create variadic functions that take a varying amount of arguments.

Get Unlimited Access Now

Transcript from the "Functions & Variadic Functions" Lesson

>> So, basic syntax kind of out of the way, variables, types, things like that, which kind of seems familiar to JavaScript. The next section I'd like to talk about are some of the more complex structures that go has and JavaScript is not. So functions, although they start out looking a little similar, they take arguments and they return values.

[00:00:19] The function signatures and how they're defined are a little bit unique to go. So, as you see on the slide here, our function print age takes in one argument. It's called age, it's named age and it is set to an integer type. And that word to the right of the closing parentheses, that second int indicates what type is function must return.

[00:00:38] It's not optional, every time this function gets called, it must return an integer value. And then the code within the curly braces will be the code that we execute within that function. But you will notice that last line return, we're returning out the value of age which needs to be an integer.

[00:00:53] So you'll notice that so far in our main function we haven't been passing at anything and we haven't been returning anything and that's because as that main entry point of our application, it's job is just run your whole program. You're not gonna be passing at anything and it doesn't need to do anything with the value at the end of the day.

[00:01:08] So the next function is we're gonna be writing will be a little bit more complex, involving some of these arguments and return values. So let's hop over to functions.go, Okay, so we're in functions.go and the fourth section of complex structures. And if we go ahead and copy that first function we saw in the slide, which is print age, it took that age argument which was an integer.

[00:01:51] We wanted it to return an integer and then execute some code within the curly braces here. The code we want, we're just gonna go ahead and print out that age. But then because our function is requiring us to return an integer, we need to return age at the end of the day.

[00:02:10] In order to call that code, we can go in our main function here and we'll call printAge and hand it some sort of integer. If i run the program now, so go run or in section four in the code directory and functions.go, you'll see that we print out our age, everyone's happy.

[00:02:34] If necessary, go also allows us to return more than one thing. So we can add multiple return values of a function by putting them in parentheses, still between our first argument parentheses block and then our opening curly brace. And so now if I were to save this and run this file, it's actually working.

[00:02:54] I don't know if you caught that but when I hit Save, my linter helped me out there and it inserted a default value. So that go wouldn't get mad. So I can't actually save it and make this go without that at the moment. But this will require our function to now return two integers rather than one.

[00:03:11] And if we weren't to do that in an actual runtime environment, we would get an error. Another interesting aspect of go is if we wanted to, over here we just have some blank integers. We could also actually name our return values. And modify the code within our print age function a little bit.

[00:03:28] So let's say our first one is gonna be age of Sally. Our second one is the age of Bob. And then in our function here, let's say we had a couple values that actually pertained to both Sally and Bob. So you'll notice here that I don't have to use the colon equal sign, the syntax of this where I have the name of the variable on the left and then the value on the right where there's no var keyword, there's no colon equal sign, and there's no type name.

[00:04:01] Indicates that somewhere in this function those variables have already been declared. And even though they're in the return values, I'm still saying hey, go instantiate a couple placeholders of two variables with these names and these types, which means I can reassign that value in the function without go getting mad.

[00:04:18] So I've got a job Bob is 21, age of Sally, we'll make her 16. And then if I want to, I can actually just kill all of this, running the code. I'm missing a return at the end of the function, because I do need the word return. But I don't have to explicitly now say return age of Bob, age of Sally.

[00:04:41] I'm gonna modify this to show the actual return value that we get within print age. So with that print line function added to our main, you see that we are now returning two different values 16 and 21. Although our print age is still expecting an age integer, we're not doing anything with it, which is a contrived example.

[00:05:08] That doesn't make a lot of sense. But ultimately, what this function allows us to do is redefine two variables that we expect this function to return and then return them. Kind of implicitly without having to specify that in our return statement on line eight
>> So the main function prints 8, is there a way to make that print 21 and 16?

>> So it actually is printing 21 and 16, the 8 is a little bit confusing here cuz we're no longer doing anything with that in the function. If I were to kill eight right now, I'm going to get an error because there are not enough arguments in call to print age.

[00:05:47] And that's because right now we're still expecting print age to exhort, like, except an age integer. If I go ahead and kill this up here, resave and then rerun our function it makes a little bit more sense. It's not expecting any age argument but we're still printing out age of Sally age of Bob.

[00:06:04] So you'll notice that our function is returning two different values, we've got 16 and 21. We're not really doing anything with that on line 12 here, but let's re-factor this and we're going to set up the syntax where you can save and declare two variables at the same time.

[00:06:20] So knowing that print age returns two things based on our function signature here, we know it does return two integers. We can say hey, age one, age two, I wanna assign you both, both of you values to the result of the print age function. So because print age returns two things, we can immediately assign two variables, those values respectively.

[00:06:48] And then if I wanted to print them down here instead, I'm going to print line age one, and then age two. The last example of functions I'd like to talk about real quick is the concept of a varriatic function. And so right now we have print age accepting no values.

[00:07:14] I'm gonna modify this to have it return, just an integer or kind of got to back things up a little bit. [SOUND] cool, and then let's say I wanna call print age and I'm passing it a few different arguments. All of which are numbers. Let's make these actually ages, how about 16, 21 and 30.

[00:07:49] Age is accepting three different integers. I could either [SOUND] specify in my function signature that print age should accept three different ages, all of which are integers, and then return some sort of integer at the end of the day. But if I'm not sure how many integers I'm passing, I just know I wanna do something with all of them.

[00:08:12] It also provides a similar kind of spread operator Which would look something like this, where the first argument represents the collection of things you're passing and the syntax on the right indicates that it's gonna be some unknown quantity of integers that you're dealing with. And you can behave the same way inside here.

[00:08:33] And then at this point, ages becomes a collection and so you'd iterate over it similar to the for loop with the range key word. Okay, so since we've refactored our function to accept a spread operator kind of thing, a variadic function, a set of ages. We're gonna remove the int here since that doesn't make sense anymore.

[00:08:52] And let's iterate over the ages and print those out one by one. So for that we're gonna go back to our for loop. We're not gonna need the index, I'm gonna automatically put that underscore in there. A second argument is the value. And it's assigned it to a value of the range keyword and then what we're iterating over which here is the ages.

[00:09:10] The set of ages that this function will accept. So within this function, I'm going to just print out [SOUND] the value. But you'll notice that now because I've modified what's happening in this function, it's no longer returning an integer. So in order for this function signature to be accurate, we'll also need to remove what that function returns.

[00:09:41] So if I run that file once again, you'll now see each of those print out individually but I no longer have to be super specific about what I pass into this function. It gives me a little bit more freedom to not know what's happening in the program.