WEBVTT 0 00:01.560 --> 00:02.930 Welcome back! 1 00:02.970 --> 00:07.240 In this part you're going to learn about functions and pointers. 2 00:07.290 --> 00:11.570 All right let's start talking about the road map in this section. 3 00:11.620 --> 00:13.540 You're going to learn about functions. 4 00:13.540 --> 00:18.190 Functions are reusable code blocks that you can call any time you want. 5 00:18.190 --> 00:21.250 You've been using them since the start of the course. 6 00:21.250 --> 00:27.380 Now it's time to create your own functions in the next section you're going to learn about pointers 7 00:27.410 --> 00:34.780 as well. A pointer store a memory address of a value such as a variable you can use pointers to manipulate 8 00:34.780 --> 00:38.690 values indirectly through their memory addresses. 9 00:38.740 --> 00:44.280 So at the end of this section you're going to rewrite the log parser project using functions. 10 00:44.350 --> 00:48.730 You'll understand the basics of designing a program using functions. 11 00:49.000 --> 00:54.570 You're also going to learn about the past by value semantics so you'll understand the implications of 12 00:54.570 --> 00:59.260 passing struct, map and slice values to functions. 13 00:59.260 --> 01:01.690 All right let's get started. 14 01:01.830 --> 01:05.490 Now let's remember what a function looks like. 15 01:05.490 --> 01:08.880 Every function starts with func keyword. 16 01:09.150 --> 01:15.100 And it has a body that is wrapped between curly braces between these curly braces. 17 01:15.120 --> 01:18.590 You write your code after the func keyword. 18 01:18.630 --> 01:20.550 You need to give it a name. 19 01:20.580 --> 01:26.700 There are some special functions called init and main if you don't want to hard to debug problems I 20 01:26.700 --> 01:34.760 recommend you not to use their names in your custom functions by the way a function may declare zero or 21 01:34.760 --> 01:36.580 more input parameters. 22 01:36.590 --> 01:40.610 Right now this function doesn't have any input parameters. 23 01:40.610 --> 01:44.550 So there are only parentheses for example. 24 01:44.610 --> 01:52.760 This function doesn't declare any input parameters functions can also take multiple input parameters 25 01:52.880 --> 01:56.990 like so each parameter should have a name and a type. 26 01:57.110 --> 02:01.010 For example here is a function that adds two numbers together. 27 02:01.130 --> 02:06.680 It has two input parameters and both are integers. 28 02:06.700 --> 02:08.590 You can also type it like so. 29 02:08.590 --> 02:12.030 So those two declarations are actually equal. 30 02:12.130 --> 02:20.600 If you have noticed it works just like a parallel variable declaration if a function doesn't declare 31 02:20.690 --> 02:27.350 any result values then you can call the return statement without any arguments like so by the way as 32 02:27.350 --> 02:32.540 you know the return statement causes a function to stop and terminate immediately. 33 02:32.570 --> 02:39.170 For example in this function when the return statement is executed the function won't execute the rest 34 02:39.170 --> 02:46.920 of the code however most of the time a function may declare single or multiple result values as well. 35 02:46.980 --> 02:53.130 You just need to declare a list of types like so separated by commas for example let's take a look at 36 02:53.130 --> 02:59.010 the Atoi function, you know it very well. It takes a single input parameter and it returns two result 37 02:59.010 --> 03:07.050 values an integer and an error as a convention you return an error value always declare it as the last 38 03:07.050 --> 03:14.270 parameter when a function declares the result parameters it needs to return them as well. 39 03:14.480 --> 03:21.210 To do that you need to use the return statement like so here this function returns multiple values. 40 03:21.260 --> 03:28.040 So you need to separate them using a comma as you can see the number of values next to the return statement 41 03:28.190 --> 03:35.630 matches the number of the result values when a function calls the Atoi function the returned values will be 42 03:35.630 --> 03:39.120 copied to the caller function. 43 03:39.260 --> 03:45.680 By the way if there is a single result value you don't need to wrap it inside parentheses for example 44 03:45.860 --> 03:50.630 this function takes an int value and returns a single string value. 45 03:50.630 --> 03:56.840 Notice that you don't need to use a comma when returning a single result value okay. 46 03:56.860 --> 04:03.810 Now let's take a look at an example let's say you have two variables like so you want to multiply them 47 04:03.810 --> 04:10.230 using a function like so as you can see unlike the main function you need to call your functions by 48 04:10.230 --> 04:12.690 yourself or Go want execute them. 49 04:13.080 --> 04:16.860 Let's take a look at what the multiply function looks like. 50 04:16.920 --> 04:24.240 It accepts 2 input parameters and it returns a single result value let's execute the function the values 51 04:24.270 --> 04:31.260 go into the function behind the scenes the function creates two new variables and assigns them the 52 04:31.260 --> 04:32.790 input values. 53 04:32.790 --> 04:37.790 So the variable a contains 5 and the variable b contains 2. 54 04:37.860 --> 04:44.640 After that, the function multiplies the variable a with the variable b, and saves the result back into 55 04:44.640 --> 04:45.860 the variable a. 56 04:46.200 --> 04:54.830 Lastly it returns the copy of the variable a to the caller (function) like so so the variable rest at left becomes 57 04:54.860 --> 04:55.850 10. 58 04:55.970 --> 05:00.290 Notice that the variables n and m are the same. 59 05:00.330 --> 05:03.620 It's because they don't belong to the multiply function. 60 05:03.620 --> 05:11.140 So the function cannot change that the function can only change its own local copies here. 61 05:11.240 --> 05:15.250 The variables a and b are local to the function. 62 05:15.290 --> 05:19.440 So the function can only change the variables a and b. 63 05:19.640 --> 05:23.620 For example here it changes the variable a. 64 05:23.690 --> 05:26.230 After that it returns the variable a. 65 05:26.240 --> 05:28.620 However it is also a copy. 66 05:28.820 --> 05:32.470 So the return value is a clone of the local variable. 67 05:32.480 --> 05:34.900 a. Alright. 68 05:34.930 --> 05:39.170 Now let's take a look at an example in coding editor. As always, 69 05:39.190 --> 05:41.690 we'll start with the most basics. 70 05:41.710 --> 05:45.160 In the end you'll be able to write your own functions. 71 05:45.160 --> 05:49.450 In this example you're going to declare very simple functions. At the start. 72 05:49.450 --> 05:52.670 they won't take any inputs and they won't return anything. 73 05:52.690 --> 05:56.640 The goal of this lecture is learning about the function scopes. 74 05:56.740 --> 06:00.730 I'm going to show you why you should not use package level variables. 75 06:01.210 --> 06:08.320 Okay let's say you want to create a program that keeps track of how many times a website gets visited. 76 06:08.710 --> 06:13.690 To do that you create a package level variable like so as an example. 77 06:13.690 --> 06:15.950 I'm going to create a couple of functions. 78 06:16.060 --> 06:17.380 They will read and write 79 06:17.380 --> 06:23.260 to this variable. Let's declare the first function. It doesn't take an input parameters and doesn't 80 06:23.260 --> 06:28.720 return any value. It's gonna print the variable n. Let's also print the counter 81 06:31.770 --> 06:33.740 let's say you want to print a message, 82 06:33.750 --> 06:39.780 if the counter is zero like so. Okay let's see how it works. 83 06:39.920 --> 06:46.250 As you can see it doesn't print anything because as I said only the main and init functions are called 84 06:46.280 --> 06:47.450 automatically. 85 06:47.450 --> 06:52.280 So you need to call the showN function yourself like so. 86 06:52.300 --> 06:55.190 Hmm... I don't want to print the counter if it is zero right. 87 06:55.210 --> 06:57.270 However here it gets printed. 88 06:57.400 --> 07:04.470 Despite that so I'm going to go back to the showN function and here I'm going to return from it. 89 07:04.480 --> 07:11.470 Remember this function doesn't return a result value so it cannot return a value the return statement 90 07:11.470 --> 07:15.370 here only allows me to terminate the showN function. 91 07:15.370 --> 07:22.500 So the function won't execute the rest of the statements because of the return statement it doesn't execute 92 07:22.510 --> 07:24.310 the last statement. 93 07:24.310 --> 07:29.980 Now I want to declare another function to increment the counter inside the function. 94 07:30.010 --> 07:31.410 I'm going to increment it. 95 07:31.720 --> 07:37.050 Now I'm going to call the function from the main function twice. 96 07:37.110 --> 07:42.760 I'm also going to call the showN function here to print the counter. 97 07:42.920 --> 07:43.630 OK good. 98 07:43.630 --> 07:50.350 Now it says "n is two" because I called the incrN function twice by the way every declared function 99 07:50.440 --> 07:54.440 occupies the same name space within the same package. 100 07:54.550 --> 07:59.560 So you cannot redeclare a function with the same name of an existing function. 101 07:59.560 --> 08:03.310 So in the same package each function should have a unique name. 102 08:03.520 --> 08:09.580 As you can see now it says it was declared within the same scope before. By the scope, 103 08:09.580 --> 08:13.980 it means the same package by the way if you are wondering about it. 104 08:14.080 --> 08:17.740 Go doesn't support the function overloading anyway. 105 08:17.840 --> 08:18.140 OK. 106 08:18.160 --> 08:19.270 So far so good. 107 08:19.280 --> 08:22.200 Now let's learn how to work with multiple files. 108 08:22.750 --> 08:24.590 Let's create another file here. 109 08:24.610 --> 08:26.530 I'm going to name it bad.go 110 08:29.520 --> 08:33.410 and I'm going to move all the functions but the main function to the new file 111 08:39.330 --> 08:40.830 Oops! It doesn't work. 112 08:40.890 --> 08:46.380 The compiler cannot see the functions magically because here I tell it to run only the main.go 113 08:46.380 --> 08:47.060 file. 114 08:47.250 --> 08:50.520 Instead I'm going to say go run dot. 115 08:50.580 --> 08:54.070 So it's gonna run every file in the current directory. 116 08:54.080 --> 09:02.380 Now if it works, good. However declaring a package variable is a bad practice as you can see this file 117 09:02.380 --> 09:08.290 can access the N variable because this file and the main.go are in the same package. 118 09:08.320 --> 09:11.000 Imagine a package with hundreds of files. 119 09:11.170 --> 09:16.090 In that case, any function or any other file can easily change the N variable. 120 09:16.360 --> 09:19.420 It can go out of control quite easily. 121 09:19.460 --> 09:25.450 It's because everyone can access and change them so you cannot easily know which one of the functions 122 09:25.490 --> 09:28.440 read and change the package level variables. 123 09:28.890 --> 09:32.210 Alright let's talk about the solution in the next lecture. 124 09:32.290 --> 09:33.990 Let's take a quick break here for now. 125 09:34.000 --> 09:34.870 See you there, bye.