1 00:00:00,000 --> 00:00:01,028 [No audio] 2 00:00:01,054 --> 00:00:04,475 As a last topic that we want to cover, how do I 3 00:00:04,500 --> 00:00:09,930 write code that is reusable and is as compact as 4 00:00:09,930 --> 00:00:15,450 possible. New developers script or any type of 5 00:00:15,450 --> 00:00:18,720 software developers have a tendency to write line 6 00:00:18,720 --> 00:00:22,470 by line linear code. So that you just have to go 7 00:00:22,470 --> 00:00:24,660 through hundreds of lines of code to figure out 8 00:00:24,660 --> 00:00:27,690 what happens. As you learn more about coding 9 00:00:27,690 --> 00:00:30,720 techniques, it's very common to take any 10 00:00:30,720 --> 00:00:33,900 functionality that you want to execute more than 11 00:00:33,900 --> 00:00:37,440 one time and put it into a code block, so you 12 00:00:37,440 --> 00:00:40,680 could just refer to it and say, go do that, go do 13 00:00:40,680 --> 00:00:43,380 that again, go do that third time, without 14 00:00:43,380 --> 00:00:46,350 having to have those lines of code in your program 15 00:00:46,350 --> 00:00:49,560 or script over and over again. So we have two 16 00:00:49,560 --> 00:00:52,140 different constructs called functions and 17 00:00:52,140 --> 00:00:55,350 procedures that we use to do exactly that. So 18 00:00:55,350 --> 00:00:57,870 let's go look at some Python examples on the 19 00:00:57,870 --> 00:01:00,660 subtle differences between functions and 20 00:01:00,660 --> 00:01:03,120 procedures, and why you might use them. Let's look 21 00:01:03,120 --> 00:01:07,255 at procedures. Here we're going to go to easypythondocs.com, 22 00:01:07,332 --> 00:01:13,320 and look at their entry for procedures. An easy example is we define a 23 00:01:13,320 --> 00:01:16,170 procedure we use the def keyword to define a 24 00:01:16,170 --> 00:01:19,170 procedure, this is called showMenu, then we just 25 00:01:19,170 --> 00:01:23,100 simply list the statements we want to execute. So 26 00:01:23,100 --> 00:01:26,370 in our program, when we just call showMenu, we're 27 00:01:26,370 --> 00:01:29,100 just basically type showMenu. It actually 28 00:01:29,100 --> 00:01:32,340 transfers control to the defined procedure, and 29 00:01:32,340 --> 00:01:34,370 then runs each of the statements in it. 30 00:01:34,394 --> 00:01:37,016 [No audio] 31 00:01:37,041 --> 00:01:41,760 So in this example, we might want to do something to show a 32 00:01:41,760 --> 00:01:44,070 menu, this is called recipeMenu, we print out 33 00:01:44,070 --> 00:01:46,740 some stuff, it returns, and then we say choice is 34 00:01:46,740 --> 00:01:49,170 equal input 'What do you want?' And so we asked the 35 00:01:49,170 --> 00:01:52,080 user, what do you want to do. You can also pass an 36 00:01:52,110 --> 00:01:55,650 argument. In this example, Example 2, we have 37 00:01:55,650 --> 00:01:58,740 storyStart, and we take an argument called name. 38 00:01:59,430 --> 00:02:02,850 So inside the procedure, we simply print Once upon 39 00:02:02,850 --> 00:02:06,750 a time, name, was imprisoned in a castle. 40 00:02:07,470 --> 00:02:09,870 They were desperate to escape, but couldn't. So 41 00:02:09,870 --> 00:02:12,930 then when we call the function, we first off 42 00:02:12,960 --> 00:02:15,690 assign userName as input, 'What is your name?', so 43 00:02:15,690 --> 00:02:17,370 the user would type in a name, and that's 44 00:02:17,370 --> 00:02:20,996 userName, then we call the procedure 45 00:02:21,143 --> 00:02:25,033 storyStart, and we pass it the name userName, 46 00:02:25,151 --> 00:02:27,660 so then it would output Once upon a 47 00:02:27,660 --> 00:02:30,990 time, whatever we typed in for name, Michael, was 48 00:02:30,990 --> 00:02:33,150 imprisoned in a castle. They were desperate to 49 00:02:33,150 --> 00:02:35,640 escape, but couldn't. So that's how a procedure 50 00:02:35,640 --> 00:02:39,090 works. It just simply transfers control, and runs 51 00:02:39,180 --> 00:02:41,760 the statements that are define for that 52 00:02:41,760 --> 00:02:43,770 procedure, and when it finishes running, it 53 00:02:43,770 --> 00:02:46,770 returns to wherever you were. We can use two 54 00:02:46,770 --> 00:02:49,530 arguments. We can use lists as arguments. We can 55 00:02:49,530 --> 00:02:51,870 use all kinds of things such as global variables, 56 00:02:52,050 --> 00:02:55,440 but that's a procedure. So the question is, how 57 00:02:55,440 --> 00:02:57,990 does that differ from a function, and there's 58 00:02:57,990 --> 00:03:01,080 really one subtle difference in what a function 59 00:03:01,080 --> 00:03:03,270 does and how it's used. So I guess that's two 60 00:03:03,270 --> 00:03:05,850 subtle differences. Let's take a look at our old 61 00:03:05,850 --> 00:03:11,379 friend, portscan.py. So if we refer back to portscan.py, 62 00:03:11,584 --> 00:03:13,860 we import our libraries, we set some 63 00:03:13,860 --> 00:03:18,480 values, and then we say def porttry and this 64 00:03:18,510 --> 00:03:21,150 looks a lot like a procedure. But it's not a 65 00:03:21,150 --> 00:03:23,640 procedure, it's a function. The reason we know 66 00:03:23,640 --> 00:03:27,480 it's a function is because functions return 67 00:03:27,515 --> 00:03:32,705 values. So we know that porttry is going to 68 00:03:32,730 --> 00:03:35,940 execute some code, but it's also going to return a 69 00:03:35,940 --> 00:03:38,482 value either True or None. 70 00:03:40,044 --> 00:03:42,575 So that's one subtle difference. Procedures don't 71 00:03:42,600 --> 00:03:46,440 return values. Functions always return a single 72 00:03:46,440 --> 00:03:51,510 value, or it by default returns a single value. If 73 00:03:51,510 --> 00:03:53,550 we then go see how it's used, remember, for a 74 00:03:53,550 --> 00:03:55,440 procedure, we simply type the name of the 75 00:03:55,440 --> 00:03:57,840 procedure, the procedure runs, and then returns 76 00:03:57,840 --> 00:03:59,970 back to where we were and goes on with the 77 00:03:59,970 --> 00:04:04,350 program. In a function, we can use a function in 78 00:04:04,380 --> 00:04:08,850 another statement. In this case, we're going to 79 00:04:08,850 --> 00:04:13,140 say value is equal to porttry. So we're going to 80 00:04:13,140 --> 00:04:16,470 run a function, the function returns a value and 81 00:04:16,470 --> 00:04:19,290 we take that value and assign it to the 82 00:04:19,290 --> 00:04:22,440 variable called value. So that's the difference 83 00:04:22,440 --> 00:04:25,260 between functions and procedures. How do you know 84 00:04:25,260 --> 00:04:27,269 which one you want to use? Well, it depends on 85 00:04:27,269 --> 00:04:30,510 what you want to do. If you want to run some code, 86 00:04:30,690 --> 00:04:34,740 and then get some output data to use, probably 87 00:04:34,740 --> 00:04:37,920 should use a function. Functions are very useful 88 00:04:38,070 --> 00:04:40,980 for returning status information, so that when you 89 00:04:40,980 --> 00:04:43,470 get the function back, if everything worked well, 90 00:04:43,495 --> 00:04:47,005 if the status is good, then you don't do anything, 91 00:04:47,130 --> 00:04:50,160 or maybe that tells you to go do something, and if 92 00:04:50,160 --> 00:04:52,650 the status is bad, you may take a different 93 00:04:52,650 --> 00:04:55,140 avenue. So if you get an error return from a 94 00:04:55,140 --> 00:04:58,650 function, it may say, 'Well, let's go try to do 95 00:04:58,650 --> 00:05:00,433 this a different way', whereas the if you get a 96 00:05:00,458 --> 00:05:03,300 positive return value, you may say, 'Okay, we got 97 00:05:03,300 --> 00:05:05,790 our data. Now let's go fetch the data and use it'. 98 00:05:06,450 --> 00:05:10,380 For a procedure, you don't expect to return value, 99 00:05:10,380 --> 00:05:13,050 you simply go do something and then continue where 100 00:05:13,050 --> 00:05:16,260 you were working from. So that is a brief 101 00:05:16,260 --> 00:05:20,370 difference between functions and procedures. Use 102 00:05:20,370 --> 00:05:23,220 them anytime that you try to do something more 103 00:05:23,220 --> 00:05:25,950 than one time, you should put that code that 104 00:05:25,950 --> 00:05:28,290 you're running more than one time into a procedure 105 00:05:28,290 --> 00:05:31,080 or function at the top of your program. So in the 106 00:05:31,080 --> 00:05:33,900 body of your program, you just simply refer to it when you need it. 107 00:05:33,924 --> 00:05:48,113 [No audio]