1 00:00:00,800 --> 00:00:03,553 - [Instructor] Passing functions in C++. 2 00:00:05,870 --> 00:00:07,840 Here's our use case. 3 00:00:07,840 --> 00:00:09,850 We've been studying hash tables, 4 00:00:09,850 --> 00:00:13,850 and soon we're going to implement hash tables in C++. 5 00:00:13,850 --> 00:00:16,880 And for the upcoming project on hash tables, 6 00:00:16,880 --> 00:00:19,530 you'll be asked to experiment with different hash functions 7 00:00:19,530 --> 00:00:21,440 to see how behavior changes 8 00:00:21,440 --> 00:00:23,090 when we change our hash function. 9 00:00:24,040 --> 00:00:25,470 Now, in order to do that, 10 00:00:25,470 --> 00:00:29,070 you could implement two different hash table classes, 11 00:00:29,070 --> 00:00:29,903 hash table A 12 00:00:29,903 --> 00:00:31,210 and hash table B, 13 00:00:31,210 --> 00:00:33,560 each with a different hash function. 14 00:00:33,560 --> 00:00:36,380 But that's gonna lead to a lot of code duplication 15 00:00:36,380 --> 00:00:38,220 because the only thing that's changing 16 00:00:38,220 --> 00:00:39,760 is the hash function itself. 17 00:00:39,760 --> 00:00:41,280 The rest of the code 18 00:00:41,280 --> 00:00:43,433 for your hash table's gonna be the same. 19 00:00:44,290 --> 00:00:46,280 So, instead what we're gonna do 20 00:00:46,280 --> 00:00:48,580 is we're gonna pass a hash function 21 00:00:48,580 --> 00:00:51,220 to the hash table constructor. 22 00:00:51,220 --> 00:00:54,600 And then the hash table object thus instantiated 23 00:00:54,600 --> 00:00:57,240 will include the hash function we passed to it 24 00:00:57,240 --> 00:00:58,653 when we created it. 25 00:01:00,040 --> 00:01:01,310 Now in order to do this, 26 00:01:01,310 --> 00:01:02,430 first we need to learn 27 00:01:02,430 --> 00:01:05,070 how to pass a function in C++. 28 00:01:05,070 --> 00:01:07,070 And C++ allows us to do that. 29 00:01:07,070 --> 00:01:10,460 Just like we can pass a variable to a constructor, 30 00:01:10,460 --> 00:01:12,810 we can pass a value to a constructor, 31 00:01:12,810 --> 00:01:15,010 we can also pass a function. 32 00:01:15,010 --> 00:01:17,620 And then that function will be incorporated 33 00:01:17,620 --> 00:01:22,280 into the object itself when it's instantiated. 34 00:01:22,280 --> 00:01:24,140 So let's first, 35 00:01:24,140 --> 00:01:24,973 like I said, 36 00:01:24,973 --> 00:01:28,003 see how this passing of functions works. 37 00:01:29,660 --> 00:01:32,500 So here we are back in CLion, 38 00:01:32,500 --> 00:01:35,020 and I'm going to create 39 00:01:35,020 --> 00:01:36,240 a new file 40 00:01:37,240 --> 00:01:39,803 and I'm gonna call it passingFunctions.cpp. 41 00:01:44,690 --> 00:01:48,780 And then we're going to use a new include 42 00:01:48,780 --> 00:01:50,200 that we haven't seen before. 43 00:01:50,200 --> 00:01:53,703 Include functional. 44 00:01:55,490 --> 00:01:58,510 And this header gives us the ability 45 00:01:58,510 --> 00:02:01,980 to pass functions around in C++. 46 00:02:01,980 --> 00:02:02,813 Now we're also, 47 00:02:02,813 --> 00:02:04,070 for the purposes of this demo, 48 00:02:04,070 --> 00:02:05,523 gonna use IO stream. 49 00:02:11,030 --> 00:02:12,583 And we're gonna use string. 50 00:02:20,100 --> 00:02:23,330 And now I'm gonna do something that I'll... 51 00:02:24,260 --> 00:02:25,234 It's cheating a little bit. 52 00:02:25,234 --> 00:02:27,430 It's not a good practice 53 00:02:27,430 --> 00:02:31,093 but for the purposes of this very short demo it's okay. 54 00:02:32,790 --> 00:02:33,900 I'm gonna define 55 00:02:33,900 --> 00:02:38,050 and implement a class right here in this file. 56 00:02:38,050 --> 00:02:40,760 Ideally, this should go in a header file. 57 00:02:40,760 --> 00:02:43,143 So do as I say not as I do. 58 00:02:44,100 --> 00:02:46,380 So without further ado, 59 00:02:46,380 --> 00:02:47,943 we're gonna create a class. 60 00:02:48,950 --> 00:02:51,333 We'll call it very creatively, MyClass. 61 00:02:55,260 --> 00:02:58,373 And then we're going to have a private member. 62 00:03:00,640 --> 00:03:03,000 And this is gonna be the function that we pass in. 63 00:03:03,000 --> 00:03:05,900 So how do we do this? 64 00:03:05,900 --> 00:03:08,040 Well, whenever we create a private member 65 00:03:08,040 --> 00:03:09,790 we have to give the type. 66 00:03:09,790 --> 00:03:13,233 And so the type here is standard function. 67 00:03:16,450 --> 00:03:17,820 And that's provided by 68 00:03:19,145 --> 00:03:22,780 the functional library that we just included. 69 00:03:22,780 --> 00:03:26,943 And this function is going to return a string. 70 00:03:30,660 --> 00:03:33,513 And it's gonna take as a parameter an integer. 71 00:03:35,240 --> 00:03:36,640 And we'll just call it func. 72 00:03:39,820 --> 00:03:41,910 So here we have a private member, 73 00:03:41,910 --> 00:03:43,410 it's a function, 74 00:03:43,410 --> 00:03:44,920 it's gonna return a string, 75 00:03:44,920 --> 00:03:46,300 it's gonna take an integer 76 00:03:46,300 --> 00:03:47,403 and it's called func. 77 00:03:48,870 --> 00:03:51,503 And now we need to implement our constructor, 78 00:03:52,900 --> 00:03:53,733 public. 79 00:03:57,000 --> 00:03:58,350 And here's our constructor, 80 00:04:01,533 --> 00:04:02,366 MyClass. 81 00:04:04,220 --> 00:04:06,210 And we're going to pass in a function. 82 00:04:06,210 --> 00:04:09,210 So it's gonna take a function as a parameter. 83 00:04:09,210 --> 00:04:10,810 Again, we have to give the type. 84 00:04:13,600 --> 00:04:15,270 It's a function, 85 00:04:15,270 --> 00:04:16,570 that takes a string 86 00:04:17,510 --> 00:04:19,410 or that returns a string I should say. 87 00:04:22,000 --> 00:04:23,880 It takes an int 88 00:04:23,880 --> 00:04:26,613 as a parameter itself, func. 89 00:04:28,410 --> 00:04:31,020 And then a very simple constructor indeed 90 00:04:31,020 --> 00:04:34,533 this func = func. 91 00:04:38,360 --> 00:04:42,710 So here we've defined this private member. 92 00:04:42,710 --> 00:04:44,260 It's a function. 93 00:04:44,260 --> 00:04:46,770 When we construct our object, 94 00:04:46,770 --> 00:04:50,360 we're going to pass in a function 95 00:04:50,360 --> 00:04:53,360 and then it's gonna be assigned to this private member func. 96 00:04:54,810 --> 00:04:58,100 And then we need some way to exercise that code. 97 00:04:58,100 --> 00:05:01,580 So I'm gonna create another function here. 98 00:05:01,580 --> 00:05:06,007 A member function that we can call standard string. 99 00:05:07,100 --> 00:05:09,233 This is gonna return a string demo, 100 00:05:10,430 --> 00:05:12,100 gonna pass in an int, 101 00:05:12,100 --> 00:05:13,263 call it X. 102 00:05:14,680 --> 00:05:17,200 And then what we're gonna do is we're gonna return 103 00:05:23,330 --> 00:05:24,883 X applied to that function. 104 00:05:26,200 --> 00:05:28,760 So when we call this function, 105 00:05:28,760 --> 00:05:31,640 when we call demo in our driver code, 106 00:05:31,640 --> 00:05:33,410 we're gonna pass in an integer 107 00:05:33,410 --> 00:05:36,610 and then it's going to pass that integer 108 00:05:36,610 --> 00:05:39,560 to whatever function it is that we passed in 109 00:05:39,560 --> 00:05:41,573 and is stored in this member. 110 00:05:43,920 --> 00:05:46,920 So now that's our class. 111 00:05:46,920 --> 00:05:48,803 Now let's create a function, 112 00:05:50,240 --> 00:05:53,270 standard string. 113 00:05:53,270 --> 00:05:55,120 Because remember our function returns a string 114 00:05:55,120 --> 00:05:56,860 and it's gonna take an integer 115 00:05:56,860 --> 00:05:58,560 and we'll just call it a function. 116 00:05:59,870 --> 00:06:01,360 again very creative. 117 00:06:01,360 --> 00:06:05,720 Int X, so it's passing in an integer X, 118 00:06:05,720 --> 00:06:07,870 and then let's make it very very simple function. 119 00:06:07,870 --> 00:06:11,683 If X modular two, 120 00:06:13,190 --> 00:06:16,587 then return, and it's gotta return a string, 121 00:06:16,587 --> 00:06:19,067 "Number is odd." 122 00:06:19,940 --> 00:06:22,007 Otherwise, return 123 00:06:27,337 --> 00:06:28,447 "Number is even." 124 00:06:31,830 --> 00:06:33,603 I gotta put semi-colons there. 125 00:06:36,260 --> 00:06:37,350 So there's our function. 126 00:06:37,350 --> 00:06:38,440 Takes in an integer. 127 00:06:38,440 --> 00:06:40,900 If the integer is odd, 128 00:06:40,900 --> 00:06:43,840 it returns the string "Number is odd." 129 00:06:43,840 --> 00:06:46,690 If the input integer is even 130 00:06:46,690 --> 00:06:48,600 it returns the string "Number is even." 131 00:06:48,600 --> 00:06:49,433 And that's it. 132 00:06:50,290 --> 00:06:52,400 So that's the function that we're going to pass 133 00:06:52,400 --> 00:06:54,980 to our constructor when we instantiate 134 00:06:54,980 --> 00:06:57,000 an object of my class type. 135 00:06:57,000 --> 00:06:59,340 Now the last thing we need to do is 136 00:06:59,340 --> 00:07:02,533 implement our main function int main, 137 00:07:04,610 --> 00:07:06,383 and that's gonna return zero. 138 00:07:08,880 --> 00:07:13,810 And then we're gonna instantiate my class object. 139 00:07:13,810 --> 00:07:18,630 My class, we'll call it MyClassObject. 140 00:07:21,406 --> 00:07:23,290 And we have to pass in the functions. 141 00:07:23,290 --> 00:07:26,300 Now here we go, a function. 142 00:07:26,300 --> 00:07:28,090 So we're not passing in a variable. 143 00:07:28,090 --> 00:07:32,880 We're not passing in an integer 144 00:07:32,880 --> 00:07:33,713 or a string, 145 00:07:33,713 --> 00:07:37,053 we're passing in this function. 146 00:07:38,980 --> 00:07:41,170 And so now, 147 00:07:41,170 --> 00:07:43,040 what did I do here? 148 00:07:43,040 --> 00:07:45,080 I don't need those braces. 149 00:07:45,080 --> 00:07:46,363 Silly me. Sorry. 150 00:07:50,689 --> 00:07:52,693 So now let's exercise our code. 151 00:07:53,810 --> 00:07:58,810 Standard string S equals MyClassObject demo. 152 00:08:05,339 --> 00:08:07,240 I'm gonna call that demo function. 153 00:08:07,240 --> 00:08:08,720 And demo, as you remember, 154 00:08:08,720 --> 00:08:10,400 is just gonna return 155 00:08:10,400 --> 00:08:14,373 the value that we pass in applied to function, so 42. 156 00:08:15,440 --> 00:08:17,917 And that should return, 157 00:08:17,917 --> 00:08:19,160 "Number is even." 158 00:08:19,160 --> 00:08:22,130 So string will become "Number is even." 159 00:08:22,130 --> 00:08:24,850 And then we'll print that out, std cout, 160 00:08:31,133 --> 00:08:36,133 std endl. 161 00:08:37,880 --> 00:08:40,430 And then let's pass it an odd number 162 00:08:40,430 --> 00:08:43,667 and verify that it returns the string "Number is odd." 163 00:08:44,570 --> 00:08:47,173 So just gonna do odd number three. 164 00:08:48,520 --> 00:08:50,003 So when we run this program, 165 00:08:52,030 --> 00:08:53,877 it should print out, 166 00:08:53,877 --> 00:08:54,887 "Number is even," 167 00:08:54,887 --> 00:08:56,147 "Number is odd." 168 00:08:57,060 --> 00:08:59,273 And now let's run pass function. 169 00:09:02,390 --> 00:09:03,223 Oh, dear. 170 00:09:04,100 --> 00:09:06,130 I have a typo. Sorry. 171 00:09:06,130 --> 00:09:07,793 I see where it is right away. 172 00:09:08,750 --> 00:09:10,730 I need another pointy bit here. 173 00:09:10,730 --> 00:09:12,140 All right. Sorry. 174 00:09:12,140 --> 00:09:13,040 Just run it again. 175 00:09:15,650 --> 00:09:16,560 Oh, dear. 176 00:09:16,560 --> 00:09:18,700 Redefinition of S, 177 00:09:18,700 --> 00:09:20,980 look at me making mistakes all over the place. 178 00:09:20,980 --> 00:09:21,903 Sorry about this. 179 00:09:22,910 --> 00:09:25,563 All right. Finally. 180 00:09:29,757 --> 00:09:31,920 "Number's even," "Number's odd." 181 00:09:31,920 --> 00:09:34,300 So again, we've created a class. 182 00:09:34,300 --> 00:09:37,890 The class has a private member that holds a function. 183 00:09:37,890 --> 00:09:40,540 We pass a function to the constructor 184 00:09:40,540 --> 00:09:43,230 that becomes incorporated into our class object 185 00:09:43,230 --> 00:09:45,110 when we instantiate. 186 00:09:45,110 --> 00:09:48,610 We can use that function anywhere within our class 187 00:09:48,610 --> 00:09:49,443 if we like, 188 00:09:49,443 --> 00:09:52,070 and that's what we're gonna do when we come to hash tables. 189 00:09:52,070 --> 00:09:55,540 We can also call it from the outside to test it. 190 00:09:55,540 --> 00:09:58,230 Here's the function that we passed in 191 00:09:58,230 --> 00:10:00,360 and here's our code to exercise it. 192 00:10:00,360 --> 00:10:02,040 That's all there is to it. 193 00:10:02,040 --> 00:10:07,030 And we'll use this when we implement hash tables. 194 00:10:07,030 --> 00:10:07,980 That's all for now.