Hey we are now at the fourth video of the section functions in the previous video.
We looked at different flow controls commonly used in go in this video.
We will first understand what does a function look like and what is an anonymous function.
Then we will look at closures.
We will also look at creating errors handling errors and returning errors.
Finally we will explore function with undetermined numbers of parameters.
A function is a small portion of code that surrounds some action you want to perform and returns one
or more values or nothing.
The other main tool for developers to maintain structure encapsulation and code readability but also
allow an experienced programmer to develop proper units tests against his or her functions.
Functions can be very simple or incredibly complex.
Usually you'll find that simpler functions are also easier to maintain test and debug.
There is also some very good advice in the world of computer science that says a function must do just
one thing but it must do it -- well.
So what does a function look like.
A function is a piece of code with its own variables and flow that doesn't affect anything outside the
opening and close brackets.
But global package or program variables.
Here's the composition of functions in go following the previous definition.
Let's take an example.
So here's the function outside the main function.
Functions can call other functions for example in our previous Hello Function we are receiving a message
argument of type String and we are calling a different function f empty dot print f.
Hello message with our argument as parameter functions can also be used as parameters when calling other
functions or be returned.
It is very important to choose a good name for your function so that it is very clear what it is about.
Without writing too many comments over it this could look a bit trivial but choosing a good name is
not so easy.
A short name must show what the function does and let the reader imagine what error it is handling or
if is doing any kind of logging within your function.
You want to do everything that a particular behavior needs but also to control expected errors and wrapping
them properly.
So the right of function is more than simply throwing a couple of lines that does what you need.
That's why it's important to write a unit test.
Make them small and concise.
Now let's see what an anonymous function is.
An anonymous function is a function without a name.
This is useful when you want to return a function from other functions.
That doesn't need a context or when you want to pass a function to a different function.
For example here we create a function that accepts 1 number and returns a function that accepts a second
number that it adds to the first one.
The second function does not have a declarative name as we have assigned it to a variable.
That is why it is said to be anonymous.
The add variable points to an anonymous function that adds one to the specific parameter.
As you can see it can be used only for the scope its parent function main and cannot be called from
anywhere else.
Anonymous functions are really powerful tools that we will use extensively on design patterns.
Let's move on and learn about closures.
Closures are something very similar to anonymous functions but even more powerful.
The key difference between them is that an anonymous function has no context within itself and a closure
has.
Let's rewrite the previous example to add an arbitrary number instead of 1.
So let me add these lines of code.
Now the ADD and variable points to a function that returns another function but the return function
has the context of the M parameter within it.
Every call to add N will create a new function with a fixed M value so we can have main add and functions
each adding a different value.
This ability of closures is very useful to create libraries or deal with functions with unsupported
types.
Another important task while creating and using functions is dealing with errors.
So let's see how to create errors handle errors and return errors.
Errors are extensively used in go.
Probably thanks to its simplicity to create an error.
Simply make a call to errors dot new string with the text you want to create on the error.
For example have a look at this line of code as we have seen before we can return errors to a function
to handle an error.
You'll see this pattern extensively and go code.
Finally let's end this video by learning about functions with undetermined number of parameters so functions
can be declared as bariatric.
This means that it is a number of arguments can vary.
What this does is to provide an array to the scope of the function that contains the argument that the
function was called with.
This is convenient if you don't want to force the user to provide an array.
When using this function let's take an example of this.
In this example we have a sum function that will return the sum of all its arguments but take a closer
look at the main function where we call sum.
As you can see now first we call sum with three arguments and then with five arguments for some functions
it doesn't matter how many arguments you pass as it treats its arguments as an array.
All in all.
So on our sum definition we simply iterate over the array to add each number to the result integer.
Have you realized that we have given a name to the returned type usually our declaration would be written
as this but you can also name the variables that you use within the function as a return value.
Naming the variable in the return type would also zero value it at the end.
You just need to return the function without value and it will take the respective variable from the
scope as returned value.
This also makes it easier to follow the mutation that the returning variable is suffering as well as
to ensure that you aren't returning a mutated argument as we return the value here from the sum function
to the main function.
Great.
In this video we explored about functions with the go language in the next video.
We will dive into arrays slices and maps.