WEBVTT 00:00.460 --> 00:05.910 Hey we are now at the fourth video of the section functions in the previous video. 00:05.970 --> 00:10.830 We looked at different flow controls commonly used in go in this video. 00:10.830 --> 00:16.830 We will first understand what does a function look like and what is an anonymous function. 00:16.830 --> 00:19.200 Then we will look at closures. 00:19.200 --> 00:23.280 We will also look at creating errors handling errors and returning errors. 00:23.280 --> 00:28.560 Finally we will explore function with undetermined numbers of parameters. 00:28.560 --> 00:34.620 A function is a small portion of code that surrounds some action you want to perform and returns one 00:34.650 --> 00:36.940 or more values or nothing. 00:36.960 --> 00:43.440 The other main tool for developers to maintain structure encapsulation and code readability but also 00:43.440 --> 00:49.790 allow an experienced programmer to develop proper units tests against his or her functions. 00:50.070 --> 00:53.620 Functions can be very simple or incredibly complex. 00:53.700 --> 00:59.400 Usually you'll find that simpler functions are also easier to maintain test and debug. 00:59.400 --> 01:05.220 There is also some very good advice in the world of computer science that says a function must do just 01:05.310 --> 01:08.580 one thing but it must do it -- well. 01:09.270 --> 01:11.790 So what does a function look like. 01:11.820 --> 01:17.400 A function is a piece of code with its own variables and flow that doesn't affect anything outside the 01:17.400 --> 01:19.470 opening and close brackets. 01:19.470 --> 01:22.620 But global package or program variables. 01:22.620 --> 01:27.330 Here's the composition of functions in go following the previous definition. 01:27.360 --> 01:28.920 Let's take an example. 01:28.950 --> 01:32.260 So here's the function outside the main function. 01:32.490 --> 01:38.940 Functions can call other functions for example in our previous Hello Function we are receiving a message 01:39.090 --> 01:45.060 argument of type String and we are calling a different function f empty dot print f. 01:45.060 --> 01:52.110 Hello message with our argument as parameter functions can also be used as parameters when calling other 01:52.110 --> 01:55.090 functions or be returned. 01:55.180 --> 02:01.150 It is very important to choose a good name for your function so that it is very clear what it is about. 02:01.150 --> 02:06.640 Without writing too many comments over it this could look a bit trivial but choosing a good name is 02:06.640 --> 02:07.730 not so easy. 02:07.840 --> 02:13.870 A short name must show what the function does and let the reader imagine what error it is handling or 02:14.020 --> 02:17.380 if is doing any kind of logging within your function. 02:17.410 --> 02:23.290 You want to do everything that a particular behavior needs but also to control expected errors and wrapping 02:23.290 --> 02:24.340 them properly. 02:24.730 --> 02:30.920 So the right of function is more than simply throwing a couple of lines that does what you need. 02:30.940 --> 02:33.920 That's why it's important to write a unit test. 02:34.030 --> 02:37.090 Make them small and concise. 02:37.090 --> 02:40.340 Now let's see what an anonymous function is. 02:40.540 --> 02:43.330 An anonymous function is a function without a name. 02:43.330 --> 02:46.370 This is useful when you want to return a function from other functions. 02:46.480 --> 02:51.540 That doesn't need a context or when you want to pass a function to a different function. 02:51.610 --> 02:57.580 For example here we create a function that accepts 1 number and returns a function that accepts a second 02:57.580 --> 03:00.420 number that it adds to the first one. 03:00.430 --> 03:05.350 The second function does not have a declarative name as we have assigned it to a variable. 03:05.350 --> 03:08.290 That is why it is said to be anonymous. 03:08.290 --> 03:13.550 The add variable points to an anonymous function that adds one to the specific parameter. 03:13.720 --> 03:20.050 As you can see it can be used only for the scope its parent function main and cannot be called from 03:20.080 --> 03:21.430 anywhere else. 03:21.550 --> 03:27.810 Anonymous functions are really powerful tools that we will use extensively on design patterns. 03:27.860 --> 03:30.390 Let's move on and learn about closures. 03:30.460 --> 03:35.530 Closures are something very similar to anonymous functions but even more powerful. 03:35.590 --> 03:41.390 The key difference between them is that an anonymous function has no context within itself and a closure 03:41.410 --> 03:42.550 has. 03:42.550 --> 03:47.470 Let's rewrite the previous example to add an arbitrary number instead of 1. 03:47.950 --> 03:51.030 So let me add these lines of code. 03:51.130 --> 03:57.610 Now the ADD and variable points to a function that returns another function but the return function 03:57.610 --> 04:00.980 has the context of the M parameter within it. 04:01.300 --> 04:09.100 Every call to add N will create a new function with a fixed M value so we can have main add and functions 04:09.460 --> 04:11.640 each adding a different value. 04:11.650 --> 04:17.500 This ability of closures is very useful to create libraries or deal with functions with unsupported 04:17.500 --> 04:18.910 types. 04:18.910 --> 04:24.490 Another important task while creating and using functions is dealing with errors. 04:24.490 --> 04:30.340 So let's see how to create errors handle errors and return errors. 04:30.340 --> 04:33.070 Errors are extensively used in go. 04:33.160 --> 04:36.440 Probably thanks to its simplicity to create an error. 04:36.490 --> 04:42.610 Simply make a call to errors dot new string with the text you want to create on the error. 04:42.760 --> 04:49.310 For example have a look at this line of code as we have seen before we can return errors to a function 04:49.640 --> 04:50.930 to handle an error. 04:50.930 --> 04:54.530 You'll see this pattern extensively and go code. 04:54.560 --> 05:01.850 Finally let's end this video by learning about functions with undetermined number of parameters so functions 05:01.850 --> 05:04.640 can be declared as bariatric. 05:04.640 --> 05:07.790 This means that it is a number of arguments can vary. 05:07.790 --> 05:12.770 What this does is to provide an array to the scope of the function that contains the argument that the 05:12.770 --> 05:14.590 function was called with. 05:14.600 --> 05:18.570 This is convenient if you don't want to force the user to provide an array. 05:18.680 --> 05:22.890 When using this function let's take an example of this. 05:23.060 --> 05:29.270 In this example we have a sum function that will return the sum of all its arguments but take a closer 05:29.270 --> 05:32.490 look at the main function where we call sum. 05:32.540 --> 05:39.260 As you can see now first we call sum with three arguments and then with five arguments for some functions 05:39.440 --> 05:44.170 it doesn't matter how many arguments you pass as it treats its arguments as an array. 05:44.180 --> 05:45.160 All in all. 05:45.380 --> 05:52.820 So on our sum definition we simply iterate over the array to add each number to the result integer. 05:52.830 --> 05:58.770 Have you realized that we have given a name to the returned type usually our declaration would be written 05:58.860 --> 06:05.380 as this but you can also name the variables that you use within the function as a return value. 06:05.610 --> 06:10.740 Naming the variable in the return type would also zero value it at the end. 06:10.740 --> 06:15.870 You just need to return the function without value and it will take the respective variable from the 06:15.870 --> 06:18.630 scope as returned value. 06:18.630 --> 06:23.580 This also makes it easier to follow the mutation that the returning variable is suffering as well as 06:23.580 --> 06:30.690 to ensure that you aren't returning a mutated argument as we return the value here from the sum function 06:30.840 --> 06:32.940 to the main function. 06:32.940 --> 06:33.490 Great. 06:33.570 --> 06:38.610 In this video we explored about functions with the go language in the next video. 06:38.670 --> 06:42.270 We will dive into arrays slices and maps.