1 00:00:00,000 --> 00:00:05,566 [No Audio] 2 00:00:05,567 --> 00:00:08,666 This tutorial is about Function Pointer types, 3 00:00:08,800 --> 00:00:10,679 which are used to refer to a function 4 00:00:10,680 --> 00:00:13,019 whose identity is not necessarily known at 5 00:00:13,020 --> 00:00:15,989 compile time. As opposed to referencing a 6 00:00:15,990 --> 00:00:18,209 data value, a function pointer points to 7 00:00:18,210 --> 00:00:21,269 executable code within a memory. The function 8 00:00:21,270 --> 00:00:23,609 type allows us to define a variable which 9 00:00:23,610 --> 00:00:26,159 will be of type function which means that, it 10 00:00:26,160 --> 00:00:28,409 will be pointing to some function and then we 11 00:00:28,410 --> 00:00:30,749 can change the pointer in runtime to change 12 00:00:30,779 --> 00:00:34,349 it to, point to some other function. The topic 13 00:00:34,350 --> 00:00:36,629 is not much complex, and simple and therefore, 14 00:00:36,630 --> 00:00:39,179 this tutorial will be a bit short. With this, 15 00:00:39,180 --> 00:00:41,533 let's start to learn about Function Types. 16 00:00:42,666 --> 00:00:45,299 We will first define a couple of functions, I 17 00:00:45,300 --> 00:00:47,849 will define a max and min function, which will 18 00:00:47,850 --> 00:00:50,279 compute, and return the maximum and minimum of 19 00:00:50,280 --> 00:00:51,766 the two input values. 20 00:00:52,831 --> 00:00:54,972 First, we will define the max function. 21 00:00:54,973 --> 00:01:02,580 [No Audio] 22 00:01:02,581 --> 00:01:04,410 Next, I will define our main function 23 00:01:04,411 --> 00:01:05,633 in the very same way. 24 00:01:05,639 --> 00:01:11,700 [No Audio] 25 00:01:11,701 --> 00:01:14,280 Let us use these functions now 26 00:01:14,281 --> 00:01:15,617 using function pointers. 27 00:01:15,618 --> 00:01:18,533 [No Audio] 28 00:01:18,534 --> 00:01:19,500 I will first define 29 00:01:19,501 --> 00:01:21,180 a variable of type function, and we'll 30 00:01:21,181 --> 00:01:23,500 initialize it from the function of main. 31 00:01:24,566 --> 00:01:26,430 You may note that, the type of the variable is 32 00:01:26,431 --> 00:01:28,560 being automatically set by the Rust compiler 33 00:01:28,561 --> 00:01:30,600 to that of the function signature, containing 34 00:01:30,601 --> 00:01:32,666 the information of the inputs and outputs. 35 00:01:33,300 --> 00:01:35,370 We can now use the same variable to call a 36 00:01:35,371 --> 00:01:37,470 function, I will call the function in the 37 00:01:37,471 --> 00:01:39,139 print statement with some inputs. 38 00:01:39,140 --> 00:01:45,300 [No Audio] 39 00:01:45,301 --> 00:01:47,730 The Rust will interpret the variable of f as 40 00:01:47,760 --> 00:01:49,620 a function and the value inside the 41 00:01:49,621 --> 00:01:51,700 parentheses as input to the function. 42 00:01:52,000 --> 00:01:53,366 Let us execute the code. 43 00:01:53,367 --> 00:01:56,292 [No Audio] 44 00:01:56,293 --> 00:01:57,450 It displays correctly the 45 00:01:57,451 --> 00:02:00,900 minimum of the two values, now let us change 46 00:02:00,901 --> 00:02:02,133 the value of the variable to 47 00:02:02,134 --> 00:02:03,477 that of max and execute again. 48 00:02:03,478 --> 00:02:10,470 [No Audio] 49 00:02:10,471 --> 00:02:12,450 You may note that the function of maximum is 50 00:02:12,451 --> 00:02:14,670 now being executed at the maximum of the two 51 00:02:14,671 --> 00:02:17,580 values is being displayed to us. This way, we 52 00:02:17,581 --> 00:02:19,470 can change the value of the function variable 53 00:02:19,471 --> 00:02:21,570 to refer to different functions using a 54 00:02:21,571 --> 00:02:25,140 single variable. Okay now let us see some of 55 00:02:25,141 --> 00:02:27,690 the uses of the function types. We will first 56 00:02:27,691 --> 00:02:29,310 look at the case, where we can use the 57 00:02:29,311 --> 00:02:31,680 function pointer as an argument or parameter 58 00:02:31,681 --> 00:02:35,190 to another function. I will first create a 59 00:02:35,191 --> 00:02:37,500 simple function for printing an input string. 60 00:02:37,513 --> 00:02:46,470 [No Audio] 61 00:02:46,471 --> 00:02:48,390 Next, I will define another function called 62 00:02:48,391 --> 00:02:50,358 fn print_full_info. 63 00:02:50,359 --> 00:02:54,333 [No Audio] 64 00:02:54,334 --> 00:02:57,251 The first parameter of this function will be a function type. 65 00:02:57,252 --> 00:02:59,866 [No Audio] 66 00:02:59,867 --> 00:03:01,566 The second input will be a string 67 00:03:02,933 --> 00:03:05,376 and the last input will be the age. 68 00:03:05,377 --> 00:03:07,800 [No Audio] 69 00:03:07,801 --> 00:03:09,690 Inside the function, we will first call the 70 00:03:09,691 --> 00:03:11,760 fn prints_name using the 71 00:03:11,761 --> 00:03:14,280 function pointer which is f, and we'll pass 72 00:03:14,281 --> 00:03:16,766 the input string of some_one to it. 73 00:03:16,767 --> 00:03:22,229 [No Audio] 74 00:03:22,230 --> 00:03:24,419 Next, I will add an additional print statement 75 00:03:24,420 --> 00:03:25,983 for printing the age of the person. 76 00:03:25,984 --> 00:03:31,800 [No Audio] 77 00:03:31,801 --> 00:03:33,930 I will now call these functions from the main. 78 00:03:34,920 --> 00:03:36,540 I will first define a couple of variables 79 00:03:36,541 --> 00:03:37,956 containing the name and age. 80 00:03:37,957 --> 00:03:44,340 [No Audio] 81 00:03:44,341 --> 00:03:48,566 Next, I will call the fn prints_full_info with suitable inputs. 82 00:03:48,567 --> 00:03:53,833 [No Audio] 83 00:03:53,834 --> 00:03:55,551 Let us cargo run this to see the outputs. 84 00:03:55,552 --> 00:03:59,866 [No Audio] 85 00:03:59,867 --> 00:04:04,140 The fn print_full_info first calls the function of 86 00:04:04,141 --> 00:04:07,133 prints_name using the functional pointer of f n 87 00:04:07,300 --> 00:04:09,210 and then it next prints my name. 88 00:04:09,690 --> 00:04:11,610 Let us cover some more examples and details 89 00:04:11,611 --> 00:04:13,950 about function pointers. In this example we 90 00:04:13,951 --> 00:04:16,140 will define a function called add_one and 91 00:04:16,141 --> 00:04:18,766 we'll call it from another function called twice. 92 00:04:19,766 --> 00:04:21,510 First, I will define the add_one 93 00:04:21,511 --> 00:04:23,730 function, which will do nothing but add one to the 94 00:04:23,731 --> 00:04:25,828 value that is being passed to it as an input. 95 00:04:25,829 --> 00:04:32,670 [No Audio] 96 00:04:32,671 --> 00:04:35,130 Next, I will define another function 97 00:04:35,131 --> 00:04:37,380 which will call this function twice. So I 98 00:04:37,381 --> 00:04:39,733 will call this function as do_twice. 99 00:04:39,735 --> 00:04:42,900 [No Audio] 100 00:04:42,901 --> 00:04:44,430 The input to the function will be a 101 00:04:44,431 --> 00:04:47,280 functional pointer in i32 value and the 102 00:04:47,281 --> 00:04:48,926 output is i32 value. 103 00:04:48,927 --> 00:04:56,040 [No Audio] 104 00:04:56,041 --> 00:04:58,380 Inside the body of the function, I will call 105 00:04:58,381 --> 00:05:00,540 the fn add_one twice, and 106 00:05:00,541 --> 00:05:02,340 we'll add the values together using the 107 00:05:02,341 --> 00:05:03,798 function pointer. 108 00:05:03,799 --> 00:05:08,038 [No Audio] 109 00:05:08,039 --> 00:05:09,120 Now, any value that is 110 00:05:09,121 --> 00:05:11,040 being passed to this function, will be added 111 00:05:11,070 --> 00:05:13,666 one and then the values will be added. 112 00:05:14,371 --> 00:05:16,680 For instance, if I pass a value of let's say 10. 113 00:05:16,681 --> 00:05:18,930 And then a value of 1 will be added which 114 00:05:18,936 --> 00:05:22,026 will make its value 11. And then 11 will be 115 00:05:22,027 --> 00:05:24,766 added to itself, which will make the value equal to 22. 116 00:05:25,433 --> 00:05:26,940 Let us now use these functions 117 00:05:26,941 --> 00:05:28,950 in the main program now, I will use a 118 00:05:28,951 --> 00:05:30,750 variable which will hold the output value 119 00:05:30,751 --> 00:05:32,757 from the function do_twice. 120 00:05:32,758 --> 00:05:38,700 [No Audio] 121 00:05:38,701 --> 00:05:40,710 The function name add_one will 122 00:05:40,711 --> 00:05:42,990 serve as a type of functional pointer for the 123 00:05:42,991 --> 00:05:45,570 variable f inside the parameter list, of the 124 00:05:45,600 --> 00:05:48,390 do_twice function. Next I will 125 00:05:48,420 --> 00:05:50,040 add a simple print statement, which will 126 00:05:50,041 --> 00:05:52,051 print and have the value of the answer. 127 00:05:52,052 --> 00:05:57,100 [No Audio] 128 00:05:57,101 --> 00:05:59,490 The answer in this case will be having a 129 00:05:59,491 --> 00:06:02,010 value of 12. Because a value of 1 will be 130 00:06:02,011 --> 00:06:04,350 added to the value of 5, which will make 131 00:06:04,351 --> 00:06:06,480 its value equal to 6 and then 6 will be 132 00:06:06,481 --> 00:06:08,966 added to itself. So the final answer will be 12. 133 00:06:09,366 --> 00:06:10,918 Let us cargo run this. 134 00:06:10,919 --> 00:06:13,633 [No Audio] 135 00:06:13,634 --> 00:06:16,680 We obtained the required answer. The function pointers 136 00:06:16,681 --> 00:06:18,870 are very useful in some advanced topics such 137 00:06:18,871 --> 00:06:21,060 as communicating with other languages and 138 00:06:21,061 --> 00:06:23,880 external API's. Okay, that brings us to the 139 00:06:23,881 --> 00:06:26,010 end of this tutorial, we have learned about 140 00:06:26,011 --> 00:06:28,380 the functional pointers which provides a nice 141 00:06:28,381 --> 00:06:30,450 way for using the functions inside other 142 00:06:30,451 --> 00:06:33,266 functions are having a pointer to a function code. 143 00:06:34,233 --> 00:06:35,670 In the upcoming tutorial, we will be 144 00:06:35,671 --> 00:06:38,070 looking at the concept of iterator, so do 145 00:06:38,071 --> 00:06:39,840 come back for learning that and until next 146 00:06:39,841 --> 00:06:42,030 tutorial, happy Rust programming. 147 00:06:42,033 --> 00:06:46,400 [No Audio]