WEBVTT 1 00:00:01.970 --> 00:00:05.359 as I mentioned in the end of the previous video you can't really get that 2 00:00:05.359 --> 00:00:09.080 calculator to do anything until you understand how to write our own functions 3 00:00:09.080 --> 00:00:12.540 in Python now we've been using functions throughout the course so 4 00:00:12.540 --> 00:00:17.199 far but now it's time to actually look at how they work and importantly 5 00:00:17.199 --> 00:00:21.339 how we can create this for ourselves now in Python functions are created with the 6 00:00:21.339 --> 00:00:25.710 word dev followed by the function name and a pair of parentheses 7 00:00:25.710 --> 00:00:29.179 because the function introduces block of code that dev statement must be followed 8 00:00:29.179 --> 00:00:33.540 by a colon and I'm gonna show you just how this works with an example so lets define a 9 00:00:33.540 --> 00:00:41.860 function called python_food so.... 10 00:00:41.860 --> 00:00:48.710 and then we type the code we want in the function so.... 11 00:00:48.710 --> 00:00:56.810 and we can call the function here....notice how intelliJ has found it for us and knows 12 00:00:56.810 --> 00:01:05.990 that's a function so we put that code in their and we can run that now obviously it doesn't do a lot it 13 00:01:05.990 --> 00:01:10.090 just prints spam and eggs as you can see on the screen their but you can see that the keyboard 14 00:01:10.090 --> 00:01:14.390 dev is followed by the function as I mentioned and parenthesis and then also 15 00:01:14.390 --> 00:01:19.080 colon as well and then the actual method code you want executes is indented so the 16 00:01:19.080 --> 00:01:22.400 statements that make up the body of the function are indented in the usual way so 17 00:01:22.400 --> 00:01:26.330 as soon as the indent is back to the left margin then that new code or any new code at 18 00:01:26.330 --> 00:01:29.280 that point is no longer part of the function definition now the 19 00:01:29.280 --> 00:01:32.950 python_food function as can see on line 5 is called just like any 20 00:01:32.950 --> 00:01:37.060 other functions that we have used previously in the course and running the 21 00:01:37.060 --> 00:01:40.780 program results in a Pythonic meal appearing on the screen 22 00:01:40.780 --> 00:01:45.780 as you can see now although it is not obvious here all Python functions return a result 23 00:01:45.780 --> 00:01:50.090 that's important so if you don't tell the function what result to return then its gonna 24 00:01:50.090 --> 00:01:54.220 return none and we can see this with a simple change of the program what we're 25 00:01:54.220 --> 00:01:59.540 going to do is just add a print surround our function in a print we can 26 00:01:59.540 --> 00:02:04.260 actually see what is returned from that functions so run that you could see it still 27 00:02:04.260 --> 00:02:08.379 done the output on line 2 its is printed that out but also its return none and 28 00:02:08.379 --> 00:02:11.090 that's because we haven't defined the parameter now you wouldn't tell 29 00:02:11.090 --> 00:02:14.200 Python to print the result of the function that isn't intended to return anything 30 00:02:14.200 --> 00:02:18.510 but that example really demonstrates what happens when you call when you use a function 31 00:02:18.510 --> 00:02:22.599 call as an argument to another function so we've use print a lot now in the course 32 00:02:22.599 --> 00:02:27.019 and you probably very used to it so I'm certain you should be using that and 33 00:02:27.019 --> 00:02:29.910 taking on one or more arguments using parenthesis and then of course that 34 00:02:29.910 --> 00:02:34.360 prints them out but if one of the argument is a function call then the function is 35 00:02:34.360 --> 00:02:38.500 executed and its result is used as the argument to print which is what is 36 00:02:38.500 --> 00:02:43.489 happening here in this case you can see the print is print has outputted the word 37 00:02:43.489 --> 00:02:48.680 none so we've actually called the python_food function and 38 00:02:48.680 --> 00:02:54.060 this return none and the print after executing that function has returned the 39 00:02:54.060 --> 00:02:58.030 output that came back to it on the screen to none now obviously that's a pretty boring 40 00:02:58.030 --> 00:03:02.690 function so let's try make it a little bit more interesting so if we go back and make some 41 00:03:02.690 --> 00:03:09.920 changes to so I'm just gonna closed the run window and see if we can actually center the text 42 00:03:09.920 --> 00:03:18.959 ..... 43 00:03:18.959 --> 00:03:28.959 ......its a 44 00:03:28.959 --> 00:03:31.989 calculation to get an entry that is going to print in the middle of the screen so 45 00:03:31.989 --> 00:03:37.890 what we gonna do their now is instead of printing as we did there we are going to delete 46 00:03:37.890 --> 00:03:43.310 that we're going to print a space and multiply that by the number of spaces 47 00:03:43.310 --> 00:03:48.970 which we have calculated on line 4 so if margin and we are also going to print text 48 00:03:48.970 --> 00:03:54.110 like and we will just get rid of the print on line 8 and make it so its just calling the 49 00:03:54.110 --> 00:03:59.600 function now that is a bit more useful but now it can only print the same thing o its called but lets just run it first and 50 00:03:59.600 --> 00:04:05.060 see and you can see there now we actually got it centering and obviously you need to be in a 51 00:04:05.060 --> 00:04:09.160 window with 80 characters across to see it centered correctly but you can see 52 00:04:09.160 --> 00:04:14.980 how it is their is some calculation and is trying to figure out the center if I move this window a bit 53 00:04:14.980 --> 00:04:18.930 to roughly simulate 80 characters that will be roughly the middle as you can see there 54 00:04:18.930 --> 00:04:23.270 we can actually test that by changing this and by adding a parameter and instead of 55 00:04:23.270 --> 00:04:28.160 printing the hard coded it on line 3 we can print the parameter that was passed 56 00:04:28.160 --> 00:04:32.169 to this methods so what we'll do is we'll create a new method so we are going to leave 57 00:04:32.169 --> 00:04:42.160 the python food method their and create another one I'm going to type.... 58 00:04:42.160 --> 00:04:50.890 ..... 59 00:04:50.890 --> 00:04:55.940 ...... 60 00:04:55.940 --> 00:05:06.600 ..... 61 00:05:06.600 --> 00:05:12.260 ...... 62 00:05:12.260 --> 00:05:30.440 ..... 63 00:05:30.440 --> 00:05:34.570 we are now going to call the function that we've created three times with a different 64 00:05:34.570 --> 00:05:38.100 parameter which is going to be automatically place in the text variable 65 00:05:38.100 --> 00:05:42.500 and we're using that to figure out how why to format it all and we should see if we 66 00:05:42.500 --> 00:05:46.540 run this in that window that I saw that I previously that it will be 67 00:05:46.540 --> 00:05:50.600 re-centered on that screen incidentally just while I remember the Python style is 68 00:05:50.600 --> 00:05:54.620 to have to blank lines before a function definition and IntelliJ will actually give you 69 00:05:54.620 --> 00:05:59.530 warning if you only leave one so if you take this back you can see how that's 70 00:05:59.530 --> 00:06:04.630 actually complaining and go back and have a look at the code expected 2 bank lines found one so it 71 00:06:04.630 --> 00:06:08.900 know their should be 2 so if we press enter their we should get rid of that warning and then 72 00:06:08.900 --> 00:06:09.580 we'll be good to go 73 00:06:09.580 --> 00:06:15.110 ok so let's try running this and you can see their the text is now correctly 74 00:06:15.110 --> 00:06:19.860 centered now in terms of defining the argument or parameter whichever way you want 75 00:06:19.860 --> 00:06:23.540 to call it could be called anything we happen to call text which happened to 76 00:06:23.540 --> 00:06:28.950 coincide with the variable name text on line 3 so we used the same name but that can 77 00:06:28.950 --> 00:06:32.620 literally be anything you wanted it to be as long as it's a valid variable name 78 00:06:32.620 --> 00:06:35.430 obviously the text makes sense here because that's what we're doing is we 79 00:06:35.430 --> 00:06:40.560 are formatting and printing it out some texts so I decided to stick with that now when centered 80 00:06:40.560 --> 00:06:44.240 text is called it needs a single argument between the parenthesis which you can see 81 00:06:44.240 --> 00:06:48.840 down here on lines 14 through 16 this argument used to provide the actual value that 82 00:06:48.840 --> 00:06:52.450 the parameter text is set to when the function runs so the first time 83 00:06:52.450 --> 00:06:58.100 we call it as you saw if we just run again you can see the first time was spam and eggs and I've centered 84 00:06:58.100 --> 00:07:03.580 that then next we went through did another call to its spam spam and eggs and then spam 85 00:07:03.580 --> 00:07:08.080 spam spam and spam and it correctly figured out the width for each one how 86 00:07:08.080 --> 00:07:13.040 wide it needed to be to center it on the screen and worked each time for us so each 87 00:07:13.040 --> 00:07:18.020 time we are calling it the argument that is being passed to the method to the function is 88 00:07:18.020 --> 00:07:21.190 changing the different value and its calculating that and printing it on the screen 89 00:07:21.190 --> 00:07:26.370 and I've use the term parameter an argument you've heard me both say those in this video 90 00:07:26.370 --> 00:07:30.120 just to be clear parameters refers to the variables defined in the function 91 00:07:30.120 --> 00:07:34.260 definition that's that is here the definition on line 8 and also the one on line 1 92 00:07:34.260 --> 00:07:38.870 although that hasn't got a parameter their and argument refers to the actual values used when the 93 00:07:38.870 --> 00:07:44.320 functions is called which is these down here line 14 through 16 and may have found the two 94 00:07:44.320 --> 00:07:48.440 teams used interchangeably with parameters now often uses instead of argument technically 95 00:07:48.440 --> 00:07:52.180 speaking it's incorrect but spoken and for the matter written language is often 96 00:07:52.180 --> 00:07:56.400 littered with grammatical errors and this is just one more example so long as you are 97 00:07:56.400 --> 00:07:59.600 aware of the correct usage if your asked in a job interview for example then don't worry 98 00:07:59.600 --> 00:08:03.520 too much about it and know that a lot of code and a lot of videos that their will 99 00:08:03.520 --> 00:08:07.040 use the two terms interchangeably but again technically to be correct 100 00:08:07.040 --> 00:08:11.570 parameter is the definition it's the variables defined in the function 101 00:08:11.570 --> 00:08:15.280 definition itself as you can see on line 8 so we refer to text their 102 00:08:15.280 --> 00:08:20.920 as a parameter then the arguments are the actual values used when the function is 103 00:08:20.920 --> 00:08:25.870 called on line 14 through 16 so its good to know both of them but you can certainly use both terms 104 00:08:25.870 --> 00:08:26.970 interchangeably 105 00:08:26.970 --> 00:08:32.380 now the functions that we've created so far are really quite simple but they do demonstrate the 106 00:08:32.380 --> 00:08:36.590 basics of defining a function in Python so center_text also introduced 107 00:08:36.590 --> 00:08:40.360 the idea of function parameters and how they used but there's a lot more to 108 00:08:40.360 --> 00:08:44.630 functions the basic concept is that you write a block of code once you call it 109 00:08:44.630 --> 00:08:46.900 whenever you need to perform these actions in your program that's the 110 00:08:46.900 --> 00:08:51.070 reason that you're creating functions and this obviously saves a lot of typing but 111 00:08:51.070 --> 00:08:56.430 more importantly it makes the actual code you create much easy to maintain so if 112 00:08:56.430 --> 00:09:00.290 you find yourself writing the same lines of code repeatedly in a program that's a 113 00:09:00.290 --> 00:09:03.839 good indication that could you should really consider putting that code in 114 00:09:03.839 --> 00:09:07.839 a function now functions can often return results we mentioned that 115 00:09:07.839 --> 00:09:12.020 earlier in this video the face that all Python functions do 116 00:09:12.020 --> 00:09:17.200 return results and Python will ensure they return none if you don't specify another value 117 00:09:17.200 --> 00:09:21.260 before we look at how to do that we should really fix an error in center text 118 00:09:21.260 --> 00:09:26.620 so anyone used to printing text in Python would probably expect center_text to 119 00:09:26.620 --> 00:09:27.230 behave 120 00:09:27.230 --> 00:09:32.180 similar to print but if we try and use it to center a number we actually get an error 121 00:09:32.180 --> 00:09:39.210 so in other words if we come back here type....and we run it 122 00:09:39.210 --> 00:09:44.600 we actually get an error so the first two lines print fine as you can see their spam 123 00:09:44.600 --> 00:09:49.920 and eggs and spam spam and eggs but when the code processing got to line 16 124 00:09:49.920 --> 00:09:54.970 where we used a number as a argument we get an error on the screen as you can see object 125 00:09:54.970 --> 00:09:59.060 of type int has no len now the good thing here is that Python makes life easy 126 00:09:59.060 --> 00:10:03.029 but not forcing us to worry about variable types most of the time but there 127 00:10:03.029 --> 00:10:08.070 are times when we need to be aware of the type of the variable we are using now here 128 00:10:08.070 --> 00:10:12.180 when we get the length in characters and you can see that's the code on line 9 129 00:10:12.180 --> 00:10:17.100 using len and texts which is the variable in the parameter the argument 130 00:10:17.100 --> 00:10:20.890 we're assuming that its character length is actually meaningful but the thing to remember 131 00:10:20.890 --> 00:10:25.550 here is the numbers don't have a character length though so if that sounds odd consider 132 00:10:25.550 --> 00:10:30.680 that the string spam consist of 4 characters but we cannot really say that the 133 00:10:30.680 --> 00:10:35.709 number 12 consists of two characters remember that in a computer 12 is stored 134 00:10:35.709 --> 00:10:41.220 ....so basically in computer terms 12 would be stored as the 135 00:10:41.220 --> 00:10:45.750 binary number 1100 this is what would happen internally or in 16 bit notiation 136 00:10:45.750 --> 00:10:51.200 it would be 0000 1100 you didn't really need to know this I'm just trying to point out how they are stored 137 00:10:51.200 --> 00:10:56.320 differently so that's how a number would be stored but in hexadecimal it will be 138 00:10:56.320 --> 00:11:01.430 stored as a C so looking at it in those terms when we are not storing and using texts 139 00:11:01.430 --> 00:11:04.620 such what would really be meant by the length if we're trying to get the length 140 00:11:04.620 --> 00:11:08.430 of the number doesn't really make sense as can see so we've used the print 141 00:11:08.430 --> 00:11:12.770 function to display numeric variables and what happens when we do that is that 142 00:11:12.770 --> 00:11:16.270 Python automatically calls one of two functions to produces a string 143 00:11:16.270 --> 00:11:18.550 representation of that variable 144 00:11:18.550 --> 00:11:23.900 the two functions are STR and REPR and if string is defined for an 145 00:11:23.900 --> 00:11:27.570 object then the print function will use that otherwise it is going to use this sa REPR 146 00:11:27.570 --> 00:11:31.800 so what we're going to do is we are going to look at these two functions in more detail when we create 147 00:11:31.800 --> 00:11:34.790 our own objects later in the course but for now we can actually make a little bit of 148 00:11:34.790 --> 00:11:39.990 a change to make sure that this works so lets just delete what I added their about binary numbers and 149 00:11:39.990 --> 00:11:43.230 go back to our method now what I want to do is make a change 150 00:11:43.230 --> 00:11:52.430 here on line 9 so I want to type in.......and by doing that we should not need to 151 00:11:52.430 --> 00:11:57.620 change anything else and we should find the code now works you can see how its 152 00:11:57.620 --> 00:12:03.010 now work and what's basically happened is that by adding that code to line 9 153 00:12:03.010 --> 00:12:08.370 we are explicitly calling STR the STR function to convert the argument that was passed to 154 00:12:08.370 --> 00:12:13.150 a string and we're doing that before we attempt to get its length so consequently 155 00:12:13.150 --> 00:12:17.410 in the first two cases its already strings so nothing else has done it still a string 156 00:12:17.410 --> 00:12:22.450 but in the third case where we passed the parameters as an integer of the value 12 that 157 00:12:22.450 --> 00:12:27.610 gets converted to a string first time on line 9 so consequently on line 10 the len 158 00:12:27.610 --> 00:12:33.190 function is called is now a string that is the argument that sent to the len function 159 00:12:33.190 --> 00:12:37.160 and that's why it works so we got now a really useful function that can be used 160 00:12:37.160 --> 00:12:39.710 whenever we wanted to print text centered on the screen 161 00:12:39.710 --> 00:12:42.950 this is a assuming and 80 character screen of course but it doesn't really act 162 00:12:42.950 --> 00:12:46.130 as a for replacement for the built in print function if you remember from 163 00:12:46.130 --> 00:12:50.450 section 10 print can be used to write data to a file which is something center_text 164 00:12:50.450 --> 00:12:54.260 function clearly can't do it at the moment because it's a useful way to 165 00:12:54.260 --> 00:12:59.360 introduce optional and default parameters what we're going to do now is modify the function 166 00:12:59.360 --> 00:13:00.920 so that it could be used 167 00:13:00.920 --> 00:13:04.500 really pretty much as a direct replacement for the print function and 168 00:13:04.500 --> 00:13:08.250 we also have a look at another approach to the same problem now you already seen how 169 00:13:08.250 --> 00:13:12.590 can look at the Python source from IntelliJ by using to control click if your in a 170 00:13:12.590 --> 00:13:16.820 Linux machine or a Windows machine and on a Mac you can do the same by holding down command 171 00:13:16.820 --> 00:13:18.220 and clicking it 172 00:13:18.220 --> 00:13:20.660 so what we are gonna do is we're gonna have a look at the definition of print so I'm holding down 173 00:13:20.660 --> 00:13:25.900 command and click print so as you can see that's the 174 00:13:25.900 --> 00:13:29.400 definition for print so the first thing to note is that you can actually view the 175 00:13:29.400 --> 00:13:34.270 real source of the built-in functions the modules builtins.py 176 00:13:34.270 --> 00:13:38.100 stubs file and that what that means is that it only contains descriptions of the 177 00:13:38.100 --> 00:13:42.000 functions without any actual implementation so it's used by 178 00:13:42.000 --> 00:13:45.210 IntelliJ to provide the tool tips when you were typing print for example showing 179 00:13:45.210 --> 00:13:48.610 you what arguments but the actual implementation of the 180 00:13:48.610 --> 00:13:50.420 functions is actually written in C 181 00:13:50.420 --> 00:13:54.800 although we can't see all the source code for the print function as such we can 182 00:13:54.800 --> 00:13:55.880 see its definition 183 00:13:55.880 --> 00:13:59.820 in a doc string describing the parameters so you know its really still useful 184 00:13:59.820 --> 00:14:04.000 as you can see on the screen there now a .doc string just to be clear is a way of documenting your functions 185 00:14:04.000 --> 00:14:07.720 and we are going to be looking at them a little bit later but here everything between the 186 00:14:07.720 --> 00:14:14.370 two sets of triple quotes as you can see from the line 477 through 486 is the doc 187 00:14:14.370 --> 00:14:20.040 string print so you can see the first parameter looking at line 476 first 188 00:14:20.040 --> 00:14:24.710 parameters is called args and the fact that it's a plural rather than arg gives an indication of what the 189 00:14:24.710 --> 00:14:28.790 asterix means so if our parameter is prefixed with a stab then 190 00:14:28.790 --> 00:14:33.430 Asterix and it represents a variable number of arguments and what that really means is 191 00:14:33.430 --> 00:14:35.060 you can pass as many as you want 192 00:14:35.060 --> 00:14:38.620 separated by commas now seeing tha down a couple times in the course 193 00:14:38.620 --> 00:14:42.980 but we haven't really explained in detail so here is an example what we mean so I'm going to 194 00:14:42.980 --> 00:14:54.200 close it down for now so type in.....then this prints the 195 00:14:54.200 --> 00:14:59.000 5 arguments so if we run that first second 3 4 spam so it prints the five 196 00:14:59.000 --> 00:15:02.260 arguments on the same line automatically separating them with a space so what 197 00:15:02.260 --> 00:15:06.260 we could do is provide the same functionality to our center 198 00:15:06.260 --> 00:15:10.220 text function quite easily and will start doing that in the next video