1 00:00:05,840 --> 00:00:06,320 All right. 2 00:00:06,320 --> 00:00:08,660 Now let's talk about function. 3 00:00:09,380 --> 00:00:13,970 Functions are an important aspect in every programming language. 4 00:00:13,970 --> 00:00:19,700 We have actually already been used in a function through our program, and that is the main function. 5 00:00:19,700 --> 00:00:25,790 And remember, early on in this course, I said that the main function is the entry point to the program. 6 00:00:26,360 --> 00:00:32,870 And as you can tell by the main function, we define a function by using the F in keyword. 7 00:00:33,650 --> 00:00:40,910 So when we are creating a function, Russ is going to use what is known as snake casing as the conventional 8 00:00:40,910 --> 00:00:42,650 style for function names. 9 00:00:42,650 --> 00:00:48,650 Snake casing is all letters or lowercase and underscores are going to separate the words. 10 00:00:48,650 --> 00:00:55,250 So an example of this would be this is snake casing. 11 00:00:55,250 --> 00:01:00,800 So everything is lowercase and all the words are separated by underscores. 12 00:01:01,930 --> 00:01:08,500 So let's create a simple function that will print out a phrase from our function. 13 00:01:08,500 --> 00:01:13,540 So we're going to use the FN key word to signal the signal to rest. 14 00:01:13,540 --> 00:01:22,030 Hey, we're about to create a function and we're going to call it print phase using our snake casing. 15 00:01:22,570 --> 00:01:25,720 So following the name of our. 16 00:01:26,760 --> 00:01:27,570 Function. 17 00:01:27,570 --> 00:01:33,720 We are going to have an open and a closed parentheses and currently there's nothing in there. 18 00:01:33,720 --> 00:01:40,710 And then we're going to have an open and a closed bracket, and that's going to basically signify the. 19 00:01:41,610 --> 00:01:43,200 The scope of our function. 20 00:01:43,200 --> 00:01:49,650 So everything inside of these open and closed brackets is going to belong to our function. 21 00:01:49,650 --> 00:01:52,140 And in this case, our function is print phrase. 22 00:01:52,800 --> 00:02:00,750 So now let's go ahead and create a print statement that is going to say hello from the function. 23 00:02:03,300 --> 00:02:06,870 So now let's just go up here and to call our function. 24 00:02:06,870 --> 00:02:09,090 All we have to do is. 25 00:02:12,320 --> 00:02:16,580 Type in the name of our function and I'm going to get rid of the default hello world. 26 00:02:16,610 --> 00:02:23,450 So all we have now is just saying hey print phrase, call our function, which is the open and close 27 00:02:23,450 --> 00:02:24,170 parentheses. 28 00:02:24,170 --> 00:02:31,400 That is how you say, Hey, I want to call my function so now we can run cargo run and we see we got 29 00:02:31,430 --> 00:02:32,870 hello from the function. 30 00:02:33,170 --> 00:02:34,630 So sweet. 31 00:02:34,640 --> 00:02:36,170 We got our first function done. 32 00:02:37,250 --> 00:02:38,940 Let's continue building on that. 33 00:02:38,960 --> 00:02:48,050 So now inside these open and closed parentheses, we can add what's called arguments or parameters. 34 00:02:48,050 --> 00:02:51,380 Arguments or parameters are just interchangeable words. 35 00:02:51,560 --> 00:02:59,030 But inside of here, we can add in special variables that are part of the function signature. 36 00:02:59,060 --> 00:03:03,500 So when a function has parameters, you can provide it with concrete values. 37 00:03:03,770 --> 00:03:12,410 So let's create a variable inside of here and we'll call it phrase, and we're going to give it a string 38 00:03:12,410 --> 00:03:13,160 slice. 39 00:03:13,520 --> 00:03:15,830 Or a reference to a string. 40 00:03:16,920 --> 00:03:21,600 So inside print phrase, we're going to say, hey, I want you to print. 41 00:03:22,710 --> 00:03:24,960 Print my argument. 42 00:03:26,290 --> 00:03:27,000 All righty. 43 00:03:27,010 --> 00:03:29,230 So now we're passing in an argument. 44 00:03:29,230 --> 00:03:32,860 So down here, all we have to do now is. 45 00:03:33,690 --> 00:03:35,190 Print are variable. 46 00:03:35,860 --> 00:03:40,270 Like we've always done, which in this case our variable is phrase. 47 00:03:40,270 --> 00:03:43,750 So we're just creating our argument up here. 48 00:03:44,510 --> 00:03:48,050 Passing it down into this phrase variable. 49 00:03:49,040 --> 00:03:53,690 Through a string slice and saying, hey, I want you to print it out. 50 00:03:53,870 --> 00:03:55,820 So now if we car go run it. 51 00:03:57,630 --> 00:03:58,380 Success. 52 00:03:58,380 --> 00:04:02,310 We have successfully printed out our argument of print my argument. 53 00:04:02,730 --> 00:04:09,450 So now another functionality that functions has is we can return values from it. 54 00:04:09,990 --> 00:04:19,770 So I want to I'm going to comment all this out and let's do a fun little function of the greatest common 55 00:04:19,770 --> 00:04:23,520 denominator, a very well known little algorithm. 56 00:04:23,640 --> 00:04:32,760 And what we'll do here is we'll give two values, and we are going to return the greatest common denominator 57 00:04:32,970 --> 00:04:34,810 between those two values. 58 00:04:34,890 --> 00:04:40,860 And inside this function, we're also going to get a look at some control flow statements, which is 59 00:04:40,860 --> 00:04:43,920 what is going to be the topic of our next lecture. 60 00:04:44,680 --> 00:04:51,250 So let's call it create our function, call it GCD, and we're going to create a mutable variable, 61 00:04:51,280 --> 00:04:51,820 a. 62 00:04:53,350 --> 00:04:58,000 And give it a value of unsigned 64 integer. 63 00:04:59,320 --> 00:05:06,730 And then we're also going to create a mutable variable B and also give it an unsigned 64. 64 00:05:07,000 --> 00:05:16,090 Now to return a variable or to return a value from a function, you use the little arrow, the type 65 00:05:16,090 --> 00:05:19,990 that you want to return, which in this case is going to be unsigned 64. 66 00:05:20,200 --> 00:05:25,360 And now we fill in our function with the logic. 67 00:05:25,690 --> 00:05:27,760 So we're going to create a Y loop. 68 00:05:28,960 --> 00:05:32,230 And while a does not equal to zero. 69 00:05:34,620 --> 00:05:43,710 And then if A is less than B, and again, we will get into depth about what these control flow statements 70 00:05:43,710 --> 00:05:44,280 are. 71 00:05:44,550 --> 00:05:54,900 In the next lecture, I just want to show you kind of a little fun example of creating a function B 72 00:05:54,900 --> 00:05:56,040 equals C. 73 00:05:58,100 --> 00:05:59,600 Come down here. 74 00:06:00,840 --> 00:06:06,810 A equals A remainder B. 75 00:06:08,070 --> 00:06:09,720 And then we want to return B. 76 00:06:10,680 --> 00:06:15,180 So now all of our ears that it was saying that we had are now gone. 77 00:06:15,180 --> 00:06:18,630 So to return a value you put it at the. 78 00:06:19,440 --> 00:06:25,560 You signify it by not including a semicolon so we can return values. 79 00:06:25,980 --> 00:06:34,440 And as we continue to grow and continue our journey, you will see that you can have more than one return 80 00:06:34,440 --> 00:06:35,940 value in a function. 81 00:06:36,600 --> 00:06:40,500 But depending on your logic, only one value will be returned. 82 00:06:40,500 --> 00:06:43,230 But you can have return values in multiple places. 83 00:06:43,230 --> 00:06:49,500 And this case, it happened to be at the end of our function and we signify that it's a return value 84 00:06:49,500 --> 00:06:52,830 by leaving off the semicolon at the end. 85 00:06:53,370 --> 00:06:57,660 So now we'll go up here and we will say. 86 00:06:59,390 --> 00:07:00,060 GCD. 87 00:07:01,040 --> 00:07:03,080 We're going to pass in two parameters. 88 00:07:03,080 --> 00:07:06,710 So for our first parameter, let's pass in 20. 89 00:07:07,840 --> 00:07:12,400 And then for our second parameter, let's pass in five. 90 00:07:12,820 --> 00:07:14,860 But we also want to print this out. 91 00:07:14,860 --> 00:07:18,910 So let's just go ahead and put it inside of a print statement. 92 00:07:19,690 --> 00:07:24,160 That way we can verify our work is correct. 93 00:07:26,970 --> 00:07:29,830 And now let's run it. 94 00:07:29,850 --> 00:07:31,410 So awesome. 95 00:07:31,410 --> 00:07:34,200 We got the GCD of five, which is correct. 96 00:07:34,200 --> 00:07:39,480 So now let's just to verify, let's change it to four and make sure that it's now four. 97 00:07:39,510 --> 00:07:40,200 Awesome. 98 00:07:40,200 --> 00:07:44,490 So we now have a working greatest common denominator function. 99 00:07:44,940 --> 00:07:50,460 And since I went ahead and brought it up, I do want to just go ahead and show you how you can have 100 00:07:51,120 --> 00:07:56,430 multiple return values. 101 00:07:56,880 --> 00:08:00,720 And I want to pass in a. 102 00:08:01,800 --> 00:08:04,130 Uh, I'll call it Flag. 103 00:08:04,140 --> 00:08:05,820 I want it to be a boolean. 104 00:08:08,310 --> 00:08:12,480 I want it to turn a boolean. 105 00:08:12,810 --> 00:08:19,920 So in this case, if flag equals true. 106 00:08:21,440 --> 00:08:22,880 I want to return. 107 00:08:22,880 --> 00:08:26,420 True or else. 108 00:08:31,220 --> 00:08:32,030 False. 109 00:08:32,570 --> 00:08:38,600 So what I'm basically saying here is if the flag that I pass in is true, return true, let's return 110 00:08:38,600 --> 00:08:39,080 false. 111 00:08:39,080 --> 00:08:41,390 So we have to return values in here. 112 00:08:41,390 --> 00:08:48,050 We have a true value that can be returned and a false value, and it's going to be dependent upon what 113 00:08:48,050 --> 00:08:49,250 I pass in here. 114 00:08:49,250 --> 00:08:55,520 So just to show this to you, let me go ahead and say I want you to print out 115 00:08:59,240 --> 00:09:01,550 my multiple return value. 116 00:09:01,550 --> 00:09:07,850 And in this case, we're going to print out true in this one cargo run. 117 00:09:07,850 --> 00:09:08,990 So we have true. 118 00:09:08,990 --> 00:09:14,270 And then if we come up here and say false, we now print it out false. 119 00:09:14,270 --> 00:09:21,350 So as just an example of how you can print out multiple potential return values, it will be through 120 00:09:21,350 --> 00:09:24,800 control flow statements which we will cover in the next lecture.