WEBVTT 00:01.380 --> 00:07.380 In this venue we will learn about one of the most fundamental concept in any programming language functions 00:09.270 --> 00:15.630 functions are named section of a program that performs a specific task they can receive zero or more 00:15.630 --> 00:19.420 arguments in return zero or more values. 00:19.440 --> 00:22.890 Let's see some examples of defining functions. 00:23.160 --> 00:28.410 So the simpler one will receive no argument and return no value. 00:29.040 --> 00:35.360 We define a function by starting using the func keyword then the name of their function. 00:35.390 --> 00:37.300 This case let's see. 00:37.320 --> 00:45.310 Hello between parentheses are going to be the arguments in this case were not returned receiving any 00:45.330 --> 00:52.300 arguments and we're not returning any value in which in brackets we will define the behavior of this 00:52.300 --> 00:53.000 function. 00:53.160 --> 00:56.380 The behavior of this function is just to print Hello. 00:56.380 --> 01:00.940 So let's type print a land which is a building function. 01:00.940 --> 01:07.630 Go provide this function and let's type the argument which is gonna be hello now to call the function 01:08.090 --> 01:13.450 would just type the name hello and we open and close parentheses. 01:13.900 --> 01:17.860 Let's save this and executed it Hello. 01:18.070 --> 01:24.150 If we want to repeat the operation we just copy the line run the program again and we have Hello twice. 01:24.190 --> 01:31.660 Now let's say we want to define a function that receive arguments let's build some function which is 01:31.670 --> 01:38.590 gonna add two different numbers to func to say that we're defining a function the name of the function 01:38.590 --> 01:46.810 in this case some and let's receive two arguments two numbers x which is an integer and Y which is an 01:46.810 --> 01:47.640 integer. 01:47.650 --> 01:54.790 Now this function should return the operation of adding these two numbers so it will return an integer 01:54.790 --> 02:03.110 as well after the parentheses you can define the return value now just a tip here. 02:03.300 --> 02:05.850 Ask X and Y are from the same type. 02:05.890 --> 02:08.660 You can all meet the first type. 02:08.680 --> 02:13.780 And basically these red X and Y are from into type. 02:13.780 --> 02:16.640 Now let's write the behavior. 02:16.720 --> 02:19.810 So this function returns the value to return values. 02:19.810 --> 02:21.400 We need to write return. 02:22.000 --> 02:26.650 And now what do we want to do to add X and Y. 02:26.680 --> 02:29.680 Right now let's call this function. 02:29.950 --> 02:33.710 Some and now we can pass two different arguments. 02:33.770 --> 02:36.300 Let's say 10 and 20. 02:36.500 --> 02:41.280 And print output to print the sum of 10 and 20. 02:41.300 --> 02:43.940 We say this and we run it. 02:43.940 --> 02:45.020 So we have 30. 02:45.020 --> 02:46.940 Now let's up different numbers. 02:47.090 --> 02:59.320 Let's say one and five to 100 so we should have thirty six and one hundred two. 02:59.800 --> 03:01.550 Let's see how this works great. 03:01.570 --> 03:03.380 Thirty six hundred two. 03:03.430 --> 03:08.700 Now as I told you before functions can return 0 or more values. 03:08.710 --> 03:11.670 In this case some Jews return one value. 03:12.160 --> 03:17.240 But if we want to return more than one let's write an example to show that. 03:17.650 --> 03:21.930 So let's write our swap function swap will swap values. 03:21.940 --> 03:26.930 So if it receives X and Y it will return y next. 03:27.040 --> 03:30.850 Let's say X and Y are integers. 03:30.890 --> 03:38.230 And in this case we open parentheses and we said that we're going to return an integer and an integer 03:38.920 --> 03:46.890 so we receive X and Y but we return y coma x. 03:47.070 --> 03:47.380 Okay. 03:48.220 --> 03:49.680 So let me remove this. 03:50.440 --> 03:51.370 Okay. 03:51.440 --> 03:58.230 Now if we call swap of 10 and 20 How do we get those arguments. 03:58.270 --> 04:03.610 We will do a and b are equal to swap 10 and 20. 04:03.610 --> 04:12.650 So the value of a and the value of b are going to be defined by this function. 04:12.690 --> 04:18.100 So what we're doing here is we're calling the swap function with 10 and 20. 04:18.240 --> 04:23.320 And we know that the function is returning 20 and 10. 04:23.490 --> 04:27.520 So a should be 20 and B should be 10. 04:27.540 --> 04:38.300 So what this program will print is 20 which is the value of a here and 10 which is the value of B. 04:38.310 --> 04:47.220 Now the last thing about functions is that you can define sums thing call closures closures are functions 04:47.310 --> 04:49.070 assigned to variables. 04:49.080 --> 04:51.260 Now we just saw in the previous chapter. 04:51.810 --> 04:57.360 So let's say we want to define the sum function is gonna be local to the main function. 04:57.480 --> 05:01.790 So to do it we use the syntax we saw it recently. 05:02.490 --> 05:04.450 And we do it very similarly. 05:04.510 --> 05:07.380 The fun the arguments x and y. 05:07.920 --> 05:12.390 Which is going to be in time and the return type which is integer. 05:12.540 --> 05:19.370 And now we define our function which is going to be return x plus y to call this. 05:19.430 --> 05:23.890 It's exactly the same sum four and six. 05:24.000 --> 05:26.610 Let's execute this and we'll have the same result. 05:26.910 --> 05:28.500 So this is all four functions. 05:28.590 --> 05:29.910 Just to summarize. 05:29.910 --> 05:35.680 Remember that our name section of a program that performs a specific task they can receive zero more 05:35.680 --> 05:41.280 arguments and they can return zero more values and they can be defined as closures. 05:41.280 --> 05:47.820 Closures are very important for concurrency which we're gonna be looking into next chapters. 05:47.820 --> 05:48.240 And that's a.