WEBVTT 1 00:00:00.280 --> 00:00:01.980 line:15% Hi guys, and welcome back. 2 00:00:01.980 --> 00:00:05.230 line:15% In this video, we'll finally be learning about functions. 3 00:00:05.230 --> 00:00:09.120 We've been using functions such as print, len, or zip, 4 00:00:09.120 --> 00:00:11.310 but we haven't really learned how they work 5 00:00:11.310 --> 00:00:15.280 behind the scenes or even how we can define our own. 6 00:00:15.280 --> 00:00:17.470 So that's what we're gonna be looking at in this video. 7 00:00:17.470 --> 00:00:20.140 Let's create our first function. 8 00:00:20.140 --> 00:00:23.840 Functions always start with the def keyword. 9 00:00:23.840 --> 00:00:25.800 Just like if and for, 10 00:00:25.800 --> 00:00:27.870 def is a Python keyword, 11 00:00:27.870 --> 00:00:29.710 and after it you put a space, 12 00:00:29.710 --> 00:00:30.670 and what this tells Python 13 00:00:30.670 --> 00:00:34.390 is that you are about to define a function. 14 00:00:34.390 --> 00:00:36.800 So the next thing is the function name. 15 00:00:36.800 --> 00:00:39.760 For example, the print function is called print. 16 00:00:39.760 --> 00:00:41.620 Len function is called len. 17 00:00:41.620 --> 00:00:44.180 So here you can name your function whatever you want, 18 00:00:44.180 --> 00:00:45.753 for example, greet. 19 00:00:47.190 --> 00:00:50.430 Afterwards, you have to put a pair of brackets. 20 00:00:50.430 --> 00:00:54.090 So that's an opening bracket and a closing bracket. 21 00:00:54.090 --> 00:00:56.720 Inside the brackets, you can put some stuff, 22 00:00:56.720 --> 00:00:58.360 but we're gonna learn how to do that 23 00:00:58.360 --> 00:01:00.010 in a future video. 24 00:01:00.010 --> 00:01:03.200 After the brackets, the usual colon. 25 00:01:03.200 --> 00:01:07.350 And now of course, we have an indented block 26 00:01:07.350 --> 00:01:09.303 which will be the body of the function. 27 00:01:10.270 --> 00:01:11.839 Let me write some code in here and show you 28 00:01:11.839 --> 00:01:13.653 how functions operate. 29 00:01:15.700 --> 00:01:17.900 So I've written our usual code here 30 00:01:17.900 --> 00:01:20.360 that asks the user for their name, 31 00:01:20.360 --> 00:01:22.203 and then prints hello name. 32 00:01:23.500 --> 00:01:26.450 This is the body of the function, 33 00:01:26.450 --> 00:01:28.960 and the function is called greet. 34 00:01:28.960 --> 00:01:32.470 If we run this code such as by pressing the run button, 35 00:01:32.470 --> 00:01:35.130 Python would start running at line one 36 00:01:35.130 --> 00:01:38.150 and it would encounter this def keyword. 37 00:01:38.150 --> 00:01:39.900 When Python encounters a def keyword, 38 00:01:39.900 --> 00:01:42.170 it knows you want to define a new function, 39 00:01:42.170 --> 00:01:44.053 so it takes note of the name. 40 00:01:44.910 --> 00:01:47.440 In addition, if there was anything inside the brackets, 41 00:01:47.440 --> 00:01:48.960 it would also look at that, 42 00:01:48.960 --> 00:01:51.340 but there's nothing there for now. 43 00:01:51.340 --> 00:01:53.610 So Python knows the name of the new function 44 00:01:53.610 --> 00:01:54.790 that we've defined, 45 00:01:54.790 --> 00:01:58.960 and then Python instantly skips over the entire body. 46 00:01:58.960 --> 00:02:01.150 It does not run the body. 47 00:02:01.150 --> 00:02:05.230 So these lines here, if we run this programme, never execute. 48 00:02:05.230 --> 00:02:06.750 Nothing happens. 49 00:02:06.750 --> 00:02:10.690 Nothing is printed out because they are inside the function, 50 00:02:10.690 --> 00:02:13.080 and what we've done in this piece of code 51 00:02:13.080 --> 00:02:15.010 is we have defined the function. 52 00:02:15.010 --> 00:02:18.680 We've created it, but now we need to run it, 53 00:02:18.680 --> 00:02:22.900 so we will say greet just like that, 54 00:02:22.900 --> 00:02:25.120 and that runs your function. 55 00:02:25.120 --> 00:02:26.860 So if we press play, 56 00:02:26.860 --> 00:02:28.510 you'll now see enter your name, 57 00:02:28.510 --> 00:02:30.410 and we can enter anything we want, 58 00:02:30.410 --> 00:02:32.693 and then the print statement as well. 59 00:02:33.790 --> 00:02:35.380 You can put as much or as little code 60 00:02:35.380 --> 00:02:37.670 as you want inside a function, 61 00:02:37.670 --> 00:02:41.800 but you should prefer shorter functions over longer ones. 62 00:02:41.800 --> 00:02:44.290 That's because the longer your functions get, 63 00:02:44.290 --> 00:02:46.980 the more jumping around you're gonna see in your programmes, 64 00:02:46.980 --> 00:02:49.100 and it's gonna be a bit more difficult to read. 65 00:02:49.100 --> 00:02:51.910 And remember, you can always create more functions. 66 00:02:51.910 --> 00:02:55.900 Just make sure to un-indent and put another def statement 67 00:02:55.900 --> 00:02:58.530 if you want to create another function. 68 00:02:58.530 --> 00:03:00.710 Functions can call other functions. 69 00:03:00.710 --> 00:03:02.500 Calling is the same as executing, 70 00:03:02.500 --> 00:03:06.200 so you can always define a function and call it from here, 71 00:03:06.200 --> 00:03:07.510 and then you can define another one 72 00:03:07.510 --> 00:03:09.360 and call it somewhere else and so on. 73 00:03:10.430 --> 00:03:13.260 Something important is that you can only call a function 74 00:03:13.260 --> 00:03:15.690 after it's been defined, but not before. 75 00:03:15.690 --> 00:03:19.490 So if I move the greet to line one say, 76 00:03:19.490 --> 00:03:21.910 now we will get an error because it doesn't know 77 00:03:21.910 --> 00:03:26.570 what greet is until after it's run the def statement. 78 00:03:26.570 --> 00:03:30.090 Finally, if you try to do something like print name 79 00:03:30.090 --> 00:03:33.070 which is the variable that we defined inside the function, 80 00:03:33.070 --> 00:03:34.540 you will get an error. 81 00:03:34.540 --> 00:03:35.823 So let's try that out. 82 00:03:37.340 --> 00:03:40.430 You get an error that says that the name name, 83 00:03:40.430 --> 00:03:42.940 which is our variable, is not defined. 84 00:03:42.940 --> 00:03:45.000 Although it was defined inside the function, 85 00:03:45.000 --> 00:03:47.740 and we actually ran the function, 86 00:03:47.740 --> 00:03:50.100 variables created inside a function die 87 00:03:50.100 --> 00:03:52.020 when they reach the end of the function. 88 00:03:52.020 --> 00:03:54.010 So you can create variables in here 89 00:03:54.010 --> 00:03:57.580 that will only be used within the function itself. 90 00:03:57.580 --> 00:03:58.930 All right, that's everything for now. 91 00:03:58.930 --> 00:04:00.440 Thank you for joining me in this video. 92 00:04:00.440 --> 00:04:01.550 I hope you've learned something, 93 00:04:01.550 --> 00:04:02.890 and I'll see you in the next one. 94 00:04:02.890 --> 00:04:03.723 We're going to be using functions 95 00:04:03.723 --> 00:04:05.673 line:15% a lot throughout this course.