WEBVTT 0 00:01.170 --> 00:02.240 Welcome back. 1 00:02.280 --> 00:07.620 In this lecture I'm going to show you a couple of different styles of function declarations so you'll 2 00:07.620 --> 00:11.220 understand how to declare your own functions in Go. 3 00:11.520 --> 00:17.590 In the last lecture, you learned that using package level variables may create hard debug problems. 4 00:17.730 --> 00:23.880 In this lecture, you're going to learn how to confine the variables within the scope of a function. 5 00:23.880 --> 00:27.790 If the package variables are bad so what is the solution? 6 00:27.810 --> 00:35.230 The solution is to localize and confine every variable as much as you can into the same function. 7 00:35.280 --> 00:40.070 Let me show you a few examples in the coding editor. 8 00:40.200 --> 00:46.400 First of all let me remove this code and let me also delete the bad.go file. 9 00:46.410 --> 00:50.500 Now I'm going to put another window next to this one by clicking here. 10 00:50.520 --> 00:56.670 Now both windows show the same main.go file. So you can better see what's going on here inside 11 00:56.670 --> 00:57.750 the main function. 12 00:57.750 --> 01:01.130 I'm going to declare a local variable by doing so. 13 01:01.140 --> 01:07.050 I have confined the variable only to the main function no other function can access it but the main 14 01:07.050 --> 01:14.000 function I'm going to declare another function inside I'm going to print the main function's local variable 15 01:16.700 --> 01:20.900 as you can see it says the local variable is not declared. 16 01:20.900 --> 01:26.470 This is good because I cannot access and change it without the consent of the main function. 17 01:27.110 --> 01:33.260 Ok instead I'm going to declare an integer input parameter like so and I'm going to print the N variable 18 01:34.270 --> 01:35.650 inside the main function. 19 01:35.650 --> 01:39.790 I'm going to call it from here and I'm going to send the local variable to the show 20 01:39.790 --> 01:42.010 function, good. 21 01:42.010 --> 01:45.540 Now the show function can print the main function's local variable. 22 01:46.210 --> 01:51.430 However what the show function prints here is not the local variable of the main function. 23 01:51.430 --> 01:53.740 Let me show you what I mean here. 24 01:53.770 --> 01:55.920 I'm going to declare a function named incrWrong. 25 01:55.930 --> 01:56.410 I'm going to declare a function named incrWrong. 26 01:56.410 --> 02:02.900 Like so. It takes an integer parameter. Inside I'm going to increase it like so. 27 02:03.130 --> 02:03.490 All right. 28 02:03.510 --> 02:05.000 Let me call it from the main function. 29 02:06.840 --> 02:10.340 Here I sent the local variable to the incrWrong function. 30 02:10.440 --> 02:14.890 Let me also print it. 31 02:15.010 --> 02:17.480 Why does it print 10 instead of 11? 32 02:18.210 --> 02:22.610 It's because the n parameter here is a copy of the local (variable). 33 02:22.610 --> 02:24.280 When you execute a function, 34 02:24.280 --> 02:31.480 Go declares the input parameters as the local variables of that function. It declares a new variable named 35 02:31.650 --> 02:36.760 n, and then it initializes it with the copy of the local variable. 36 02:36.910 --> 02:39.970 That's why it cannot change the original variable. 37 02:39.970 --> 02:44.260 This is what I mean by confining variables to a function. 38 02:44.260 --> 02:45.640 So what should you do? 39 02:46.270 --> 02:50.450 Well after incrementing the variable you need to return a new copy. 40 02:50.500 --> 02:50.980 Like so. 41 02:51.530 --> 02:58.180 However it says too many arguments to return because I try to return a value but the function doesn't 42 02:58.180 --> 03:02.620 declare a result value so I cannot return from it. 43 03:02.650 --> 03:08.930 Instead let's duplicate this function and I'm going to change its name to incr here. 44 03:09.020 --> 03:14.710 I'm going to declare an integer result value like so now I can return from it because there is a result 45 03:14.710 --> 03:15.550 value. 46 03:15.550 --> 03:21.160 Now I need to call the incr function from the main function like so I'm going to print the local variable 47 03:21.250 --> 03:25.570 using this show function it prints 10 again. 48 03:25.570 --> 03:29.700 This happens because I didn't save the result of the function yet. 49 03:29.740 --> 03:30.620 Let's do that. 50 03:32.460 --> 03:35.070 It works now the local variable is 11. 51 03:35.070 --> 03:42.330 Now let's say you want to increase the counter by a multiplication factor so let's declare another function 52 03:42.450 --> 03:46.880 that takes two integers and returns a single integer like so. 53 03:46.900 --> 03:52.670 This function returns a single integer so I am also going to return a single value. 54 03:52.680 --> 03:55.710 Now I'm going to call it from the main function here. 55 03:55.740 --> 03:59.660 I want to increase the counter with a factor of five. 56 03:59.790 --> 04:02.750 And I copy the result back to the local variable. 57 04:02.760 --> 04:05.840 Let me also print it. 58 04:06.050 --> 04:11.580 Now the local variable is 55 so it prints 55. 59 04:11.660 --> 04:16.080 Now let's say you want to increase the counter with a string value like so. 60 04:16.310 --> 04:22.900 This function takes an integer and a string and it returns an integer and an error value. 61 04:23.060 --> 04:29.240 It returns an error value because the factor variable may not be converted to an integer, as 62 04:29.240 --> 04:29.830 you know. 63 04:30.160 --> 04:36.230 First I'm going to call the Atoi function to check whether the factor can be converted to an integer 64 04:36.230 --> 04:38.120 or not. Here, 65 04:38.130 --> 04:42.390 I'm going to call the incrBy function because it already does the same job. 66 04:42.420 --> 04:46.170 And lastly I'm going to return the result and the error value like so. 67 04:46.320 --> 04:52.060 Since the function returns two result values, I need to use a comma to separate the result values. 68 04:52.410 --> 04:59.320 OK it's time to use the function here inside the main(), I'm going to call the function like so. I've skipped 69 04:59.320 --> 05:02.320 the first value because I passed an incorrect value. 70 05:02.350 --> 05:04.900 So the function cannot convert it. 71 05:04.900 --> 05:08.770 Let me check for the error, and print a message like so. 72 05:09.180 --> 05:14.880 As you can see it prints an error. Let's duplicate it (the statement) and let's give it a convertible value like so 73 05:15.600 --> 05:21.850 here I don't check the error value because I know for sure that it (incrByStr) can convert this value to an integer. 74 05:21.990 --> 05:30.370 Let me also print the local variable. Now it works, and it prints 110. Here, it converts the string to 75 05:30.360 --> 05:30.840 two. 76 05:31.080 --> 05:37.020 Then it multiplies 55 by 2 and returns 110, hence the result. 77 05:37.020 --> 05:42.040 Now I'm going to show you how to chain a couple of functions together. Here, 78 05:42.090 --> 05:44.880 the incrBy function returns a single value. 79 05:44.910 --> 05:45.780 Right. 80 05:45.870 --> 05:48.900 And the show function takes an integer value. 81 05:48.900 --> 05:55.570 So I'm going to directly give the result of the incrBy function to the show function like so. It 82 05:55.610 --> 05:57.250 prints 220. 83 05:57.290 --> 06:01.310 However it doesn't show you the current value of the local variable. 84 06:01.940 --> 06:09.570 If I print the local variable here you can see that it still contains the same value 110. 85 06:09.600 --> 06:15.660 This is because here the incrBy function returns a copy of it and the show function only prints the 86 06:15.660 --> 06:16.260 copy. 87 06:16.260 --> 06:20.630 By the way, can I do the same thing using the incrByStr function? 88 06:20.640 --> 06:22.900 What do you think? Let me show you. 89 06:24.480 --> 06:29.540 It says too many arguments because the show function expects a single value. 90 06:29.580 --> 06:36.170 However, the incrByStr function returns multiple values so I cannot chain them. 91 06:36.180 --> 06:38.910 Let me show you a more useful example. 92 06:39.060 --> 06:42.370 Let's declare another function named sanitize. 93 06:42.540 --> 06:47.910 It takes two values like so and it returns an int value. 94 06:47.910 --> 06:53.850 As you can see the types of input values are the same as the incrByStr function. 95 06:53.850 --> 06:56.380 This means that I can chain them. 96 06:56.430 --> 06:59.190 I'll show you how to do that in a second. 97 06:59.190 --> 07:03.460 First let's type a little bit of code in the sanitize function. 98 07:03.480 --> 07:07.790 Let's say if there was an error you want to return zero like so. 99 07:08.070 --> 07:10.980 Otherwise you return the given value. 100 07:10.980 --> 07:13.910 First I'm going to call the sanitize function like so. 101 07:14.600 --> 07:23.900 And I'm gonna give it the result of the incrByStr function. Let me print it. As you can see, it prints 102 07:23.940 --> 07:24.650 zero. 103 07:24.860 --> 07:28.770 It's because the incrByStr function returns an error. 104 07:29.000 --> 07:36.320 So the sanitize function detects that and returns zero. In the end, I save zero to the local variable, so 105 07:36.320 --> 07:39.030 it becomes zero instead. 106 07:39.120 --> 07:41.550 Let me say 2 here. 107 07:41.550 --> 07:44.250 Now, it prints 220. Here, 108 07:44.280 --> 07:48.990 the incrBy function multiplies the copy of the local variable by 2. 109 07:49.110 --> 07:54.300 Then it returns 220, and a nil error value to the sanitize function. 110 07:54.450 --> 07:59.930 So the sanitize function sees that, so it just returns 220. 111 07:59.960 --> 08:03.110 And lastly I save the result to the local variable. 112 08:03.140 --> 08:05.830 So it becomes 220. 113 08:05.970 --> 08:07.720 Let me show you another example. 114 08:08.300 --> 08:10.450 Let's say you want to limit the result 115 08:10.460 --> 08:12.080 if it is greater than a number. 116 08:12.320 --> 08:14.890 So let's declare a function to do that. 117 08:14.930 --> 08:18.400 It takes two integers and returns an integer. 118 08:18.540 --> 08:21.520 And here is a named parameter. As you can see, 119 08:21.530 --> 08:28.460 you can also name the result values. When you do so, they become the local variables inside the function, just 120 08:28.460 --> 08:31.340 like the input parameters. Behind the scenes, 121 08:31.340 --> 08:33.670 Go declares them like so. 122 08:33.800 --> 08:36.880 So they get a zero value depending on their type. 123 08:36.890 --> 08:39.080 So the n variable here is zero. 124 08:39.560 --> 08:42.420 Okay first I'm going to change the m to the n. 125 08:42.560 --> 08:48.140 I do so because if it (n) is not greater than the limit, I want to return the number (n) as it is. 126 08:48.350 --> 08:54.680 So if the given number is greater than or equals to the limit, I'm going to limit it by setting it to 127 08:54.680 --> 08:56.150 the limit value like so. 128 08:56.390 --> 08:59.270 So the number cannot be greater than the limit. 129 08:59.510 --> 09:06.400 In the end I'm going to return from the function like so. You might say: But you told me when a function 130 09:06.400 --> 09:10.490 returns a result you need to give it to the return statement. 131 09:10.630 --> 09:17.670 And I told you the truth. Here, behind the scenes, it returns the result like so. So, I don't have to. 132 09:17.830 --> 09:24.280 I can do so because I use a named result value. Go allows you to do this. 133 09:24.490 --> 09:30.610 All right let's switch over to the main function. First, I'm going to call the incrBy function with 134 09:30.690 --> 09:42.090 a factor of 5. Let me also print it. As you can see, it prints 1100. Let's limit it to 500 by using 135 09:42.090 --> 09:49.720 the limit function like so. OK, now it prints 500 because the limit function detects that it is greater 136 09:49.720 --> 09:53.670 than 500 so it returns 500 instead. 137 09:53.830 --> 10:02.150 As an example let me increase the limit to 2000. It prints like so because, 1100 138 10:02.180 --> 10:04.010 is less than 2000. 139 10:04.140 --> 10:04.500 All right. 140 10:04.660 --> 10:05.480 That's all for now. 141 10:05.510 --> 10:06.430 See you in the next lecture.