1 00:00:00,000 --> 00:00:00,580 2 00:00:00,580 --> 00:00:02,950 >> DAVID J. MALAN: Let's now implement a function that doesn't just have a side 3 00:00:02,950 --> 00:00:05,920 effect, but instead returns a return value. 4 00:00:05,920 --> 00:00:09,150 Specifically, let's implement a function called get positive int whose 5 00:00:09,150 --> 00:00:11,490 purpose in life is to do exactly that. 6 00:00:11,490 --> 00:00:14,500 Specifically, I'd like to use this function as follows-- 7 00:00:14,500 --> 00:00:18,770 int N gets get positive int. 8 00:00:18,770 --> 00:00:23,640 And then print f, thanks for the percent i as a 9 00:00:23,640 --> 00:00:26,490 placeholder, comma, end. 10 00:00:26,490 --> 00:00:29,030 >> Now of course, get positive int doesn't yet exist. 11 00:00:29,030 --> 00:00:33,020 So let's promise to implement it by adding to the top of my file a line 12 00:00:33,020 --> 00:00:37,110 like int signifying that this function will return in int-- 13 00:00:37,110 --> 00:00:39,110 get positive int. 14 00:00:39,110 --> 00:00:43,000 And let's specify explicitly that this function will not take any input, and 15 00:00:43,000 --> 00:00:45,570 so its arguments are void. 16 00:00:45,570 --> 00:00:49,500 Let's now at the bottom of my file, simply so that I can keep main up top, 17 00:00:49,500 --> 00:00:52,220 actually implement or define this function. 18 00:00:52,220 --> 00:00:55,120 >> First we start with the same signature, so to speak-- 19 00:00:55,120 --> 00:00:59,140 int get positive int void. 20 00:00:59,140 --> 00:01:01,910 And now let's implement get positive int as follows. 21 00:01:01,910 --> 00:01:05,410 Let's declare an int, also called N but we could call it almost anything 22 00:01:05,410 --> 00:01:11,360 we'd like, do the following while some condition is true, and we'll return to 23 00:01:11,360 --> 00:01:13,030 that condition in a moment. 24 00:01:13,030 --> 00:01:20,800 Print f, please give me a positive int, and now let's use Get int from 25 00:01:20,800 --> 00:01:23,290 the CS50 library to actually get that int. 26 00:01:23,290 --> 00:01:28,210 >> But in my condition, let's do this loop so long as N is less than 1. 27 00:01:28,210 --> 00:01:31,260 In other words, so long as the user does not cooperate by providing me 28 00:01:31,260 --> 00:01:36,260 with a positive int, let me re-prompt him or her again, and again, and again 29 00:01:36,260 --> 00:01:37,720 until he or she does. 30 00:01:37,720 --> 00:01:40,360 But I'm not done yet, because at the end of this function I need to 31 00:01:40,360 --> 00:01:42,210 actually do something with that input. 32 00:01:42,210 --> 00:01:46,710 And so I'm going to go about returning it with a line like return end 33 00:01:46,710 --> 00:01:52,850 semicolon, thereby returning an actual int to main who called this function. 34 00:01:52,850 --> 00:01:56,810 Now it's worth noting that even though get positive int returns in int, it's 35 00:01:56,810 --> 00:02:00,470 certainly fine for it to return a positive int specifically. 36 00:02:00,470 --> 00:02:04,170 There isn't a special data type for positive integer specifically, so we 37 00:02:04,170 --> 00:02:06,490 simply use the built in "int." 38 00:02:06,490 --> 00:02:11,250 >> Now back at line nine, notice that I'm printing out N. But the N in this line 39 00:02:11,250 --> 00:02:13,970 belongs to the N that's declared in line eight. 40 00:02:13,970 --> 00:02:17,840 So it turns out you can absolutely have variables identically named so 41 00:02:17,840 --> 00:02:20,250 long as they exist within different scopes. 42 00:02:20,250 --> 00:02:24,950 And recall that scope is defined by the curly braces that most closely 43 00:02:24,950 --> 00:02:27,620 surround the variable that you've defined. 44 00:02:27,620 --> 00:02:30,370 >> Now let's compile and run this program. 45 00:02:30,370 --> 00:02:35,210 Make functions 1, dot slash functions 1. 46 00:02:35,210 --> 00:02:40,040 Let's give it a positive int-like 50, and it says thanks for the 50. 47 00:02:40,040 --> 00:02:43,180 Meanwhile, if we don't cooperate, running the program again, giving it 48 00:02:43,180 --> 00:02:47,550 0, I'm prompted again, or giving it negative 1, I'm prompted again. 49 00:02:47,550 --> 00:02:51,720 But if I do cooperate providing it with, say, 50, I'm thanked for the 50. 50 00:02:51,720 --> 00:02:54,590