1
00:00:05,420 --> 00:00:08,800
as I mentioned in the end of the
previous video you can't really get that

2
00:00:08,800 --> 00:00:12,530
calculator to do anything until you
understand how to write our own functions

3
00:00:12,530 --> 00:00:15,990
in Python now we've been using
functions throughout the course so

4
00:00:15,990 --> 00:00:20,640
far but now it's time to actually
look at how they work and importantly

5
00:00:20,640 --> 00:00:24,780
how we can create this for ourselves now in
Python functions are created with the

6
00:00:24,780 --> 00:00:29,160
word dev followed by the function name
and a pair of parentheses

7
00:00:29,160 --> 00:00:32,619
because the function introduces block of
code that dev statement must be followed

8
00:00:32,619 --> 00:00:36,990
by a colon and I'm gonna show you just
how this works with an example so lets define a

9
00:00:36,990 --> 00:00:45,310
function called python_food so....

10
00:00:45,310 --> 00:00:52,160
and then we type the code we want in the function so....

11
00:00:52,160 --> 00:01:00,260
and we can call the function here....notice how intelliJ has found it for us and knows

12
00:01:00,260 --> 00:01:09,440
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:09,440 --> 00:01:13,540
just prints spam and eggs as you can see on the screen their but you can see that the keyboard

14
00:01:13,540 --> 00:01:17,840
dev is followed by the function as I mentioned and parenthesis and then also

15
00:01:17,840 --> 00:01:22,530
colon as well and then the actual
method code you want executes is indented so the

16
00:01:22,530 --> 00:01:25,850
statements that make up the body of the
function are indented in the usual way so

17
00:01:25,850 --> 00:01:29,780
as soon as the indent is back to the left
margin then that new code or any new code at

18
00:01:29,780 --> 00:01:32,730
that point is no longer
part of the function definition now the

19
00:01:32,730 --> 00:01:36,400
python_food function as can
see on line 5 is called just like any

20
00:01:36,400 --> 00:01:40,510
other functions that we have used previously in the course and running the

21
00:01:40,510 --> 00:01:44,230
program results in a
Pythonic meal appearing on the screen

22
00:01:44,230 --> 00:01:49,230
as you can see now although it is not obvious here all
Python functions return a result

23
00:01:49,230 --> 00:01:53,540
that's important so if you don't tell the
function what result to return then its gonna

24
00:01:53,540 --> 00:01:57,670
return none and we can see this with a
simple change of the program what we're

25
00:01:57,670 --> 00:02:02,990
going to do is just add a print
surround our function in a print we can

26
00:02:02,990 --> 00:02:07,710
actually see what is returned from that
functions so run that you could see it still

27
00:02:07,710 --> 00:02:11,820
done the output on line 2 its is printed
that out but also its return none and

28
00:02:11,820 --> 00:02:14,540
that's because we haven't defined
the parameter now you wouldn't tell

29
00:02:14,540 --> 00:02:17,650
Python to print the result of the function that
isn't intended to return anything

30
00:02:17,650 --> 00:02:21,960
but that example really demonstrates what happens
when you call when you use a function

31
00:02:21,960 --> 00:02:26,040
call as an argument to another function
so we've use print a lot now in the course

32
00:02:26,040 --> 00:02:30,460
and you probably very used to it so I'm certain you should be using that and

33
00:02:30,460 --> 00:02:33,360
taking on one or more arguments using
parenthesis and then of course that

34
00:02:33,360 --> 00:02:37,810
prints them out but if one of the argument is
a function call then the function is

35
00:02:37,810 --> 00:02:41,950
executed and its result is used as the
argument to print which is what is

36
00:02:41,950 --> 00:02:46,930
happening here in this case you can see
the print is print has outputted the word

37
00:02:46,930 --> 00:02:52,130
none so we've actually called the
python_food function and

38
00:02:52,130 --> 00:02:57,510
this return none and the print after
executing that function has returned the

39
00:02:57,510 --> 00:03:01,480
output that came back to it on the screen
to none now obviously that's a pretty boring

40
00:03:01,480 --> 00:03:06,140
function so let's try make it a little bit more
interesting so if we go back and make some

41
00:03:06,140 --> 00:03:13,370
changes to so I'm just gonna closed the run window and
see if we can actually center the text

42
00:03:13,370 --> 00:03:22,400
.....

43
00:03:22,400 --> 00:03:32,400
......its a

44
00:03:32,400 --> 00:03:35,430
calculation to get an entry that is going
to print in the middle of the screen so

45
00:03:35,430 --> 00:03:41,340
what we gonna do their now is instead of
printing as we did there we are going to delete

46
00:03:41,340 --> 00:03:46,760
that we're going to print a space and
multiply that by the number of spaces

47
00:03:46,760 --> 00:03:52,420
which we have calculated on line 4 so if
margin and we are also going to print text

48
00:03:52,420 --> 00:03:57,560
like and we will just get rid of the print on line 8 and make it so its just calling the

49
00:03:57,560 --> 00:04:03,050
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:04:03,050 --> 00:04:08,510
see and you can see there now we actually got
it centering and obviously you need to be in a

51
00:04:08,510 --> 00:04:12,610
window with 80 characters across to see it centered correctly but you can see

52
00:04:12,610 --> 00:04:18,430
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:18,430 --> 00:04:22,380
to roughly simulate 80 characters that will be roughly the middle as you can see there

54
00:04:22,380 --> 00:04:26,720
we can actually test that by changing
this and by adding a parameter and instead of

55
00:04:26,720 --> 00:04:31,610
printing the hard coded it on line 3 we
can print the parameter that was passed

56
00:04:31,610 --> 00:04:35,610
to this methods so what we'll do is we'll
create a new method so we are going to leave

57
00:04:35,610 --> 00:04:45,610
the python food method their and create another one I'm going to type....

58
00:04:45,610 --> 00:04:54,340
.....

59
00:04:54,340 --> 00:04:59,390
......

60
00:04:59,390 --> 00:05:10,050
.....

61
00:05:10,050 --> 00:05:15,710
......

62
00:05:15,710 --> 00:05:33,890
.....

63
00:05:33,890 --> 00:05:38,020
we are now going to call the function that we've
created three times with a different

64
00:05:38,020 --> 00:05:41,550
parameter which is going to be
automatically place in the text variable

65
00:05:41,550 --> 00:05:45,950
and we're using that to figure out how
why to format it all and we should see if we

66
00:05:45,950 --> 00:05:49,990
run this in that window that I saw that I previously that it will be

67
00:05:49,990 --> 00:05:54,050
re-centered on that screen incidentally
just while I remember the Python style is

68
00:05:54,050 --> 00:05:58,070
to have to blank lines before a function
definition and IntelliJ will actually give you

69
00:05:58,070 --> 00:06:02,980
warning if you only leave one so if you
take this back you can see how that's

70
00:06:02,980 --> 00:06:08,080
actually complaining and go back and have a look at the code expected 2 bank lines found one so it

71
00:06:08,080 --> 00:06:12,350
know their should be 2 so if we press enter their we should get rid of that warning and then

72
00:06:12,350 --> 00:06:13,030
we'll be good to go

73
00:06:13,030 --> 00:06:18,560
ok so let's try running this and you
can see their the text is now correctly

74
00:06:18,560 --> 00:06:23,310
centered now in terms of defining the
argument or parameter whichever way you want

75
00:06:23,310 --> 00:06:26,990
to call it could be called anything we
happen to call  text which happened to

76
00:06:26,990 --> 00:06:32,400
coincide with the variable name text on line 3 so we
used the same name but that can

77
00:06:32,400 --> 00:06:36,070
literally be anything you wanted it to be as
long as it's a valid variable name

78
00:06:36,070 --> 00:06:38,880
obviously the text makes sense here
because that's what we're doing is we

79
00:06:38,880 --> 00:06:44,010
are formatting and printing it out some texts so I decided to stick with that now when centered

80
00:06:44,010 --> 00:06:47,690
text is called it needs a single argument
between the parenthesis which you can see

81
00:06:47,690 --> 00:06:52,290
down here on lines 14 through 16 this argument
used to provide the actual value that

82
00:06:52,290 --> 00:06:55,900
the parameter text is set to when the
function runs so the first time

83
00:06:55,900 --> 00:07:01,550
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:07:01,550 --> 00:07:07,030
that then next we went through did another
call to its spam spam and eggs and then spam

85
00:07:07,030 --> 00:07:11,530
spam spam and spam and it correctly
figured out the width for each one how

86
00:07:11,530 --> 00:07:16,490
wide it needed to be to center it on
the screen and worked each time for us so each

87
00:07:16,490 --> 00:07:21,470
time we are calling it the argument that is being
passed to the method to the function is

88
00:07:21,470 --> 00:07:24,640
changing the different value and its
calculating that and printing it on the screen

89
00:07:24,640 --> 00:07:29,820
and I've use the term parameter an argument
you've heard me both say those in this video

90
00:07:29,820 --> 00:07:33,570
just to be clear parameters refers to the
variables defined in the function

91
00:07:33,570 --> 00:07:37,710
definition that's that is here the definition on line 8 and also the one on line 1

92
00:07:37,710 --> 00:07:42,320
although that hasn't got a parameter their and argument refers
to the actual values used when the

93
00:07:42,320 --> 00:07:47,770
functions is called which is these down here
line 14 through 16 and may have found the two

94
00:07:47,770 --> 00:07:51,890
teams used interchangeably with
parameters now often uses instead of argument technically

95
00:07:51,890 --> 00:07:55,630
speaking it's incorrect but spoken and
for the matter written language is often

96
00:07:55,630 --> 00:07:59,850
littered with grammatical errors and
this is just one more example so long as you are

97
00:07:59,850 --> 00:08:03,050
aware of the correct usage if your asked in a job
interview for example then don't worry

98
00:08:03,050 --> 00:08:06,970
too much about it and know that a lot of
code and a lot of videos that their will

99
00:08:06,970 --> 00:08:10,490
use the two terms interchangeably but
again technically to be correct

100
00:08:10,490 --> 00:08:15,020
parameter is the definition it's the
variables defined in the function

101
00:08:15,020 --> 00:08:18,730
definition itself as you can see on line 8
so we refer to text their

102
00:08:18,730 --> 00:08:24,370
as a parameter then the arguments are the
actual values used when the function is

103
00:08:24,370 --> 00:08:29,320
called on line 14 through 16 so its good to know both of them
but you can certainly use both terms

104
00:08:29,320 --> 00:08:30,420
interchangeably

105
00:08:30,420 --> 00:08:35,830
now the functions that we've created so far are really
quite simple but they do demonstrate the

106
00:08:35,830 --> 00:08:40,039
basics of defining a function in Python so center_text also introduced

107
00:08:40,039 --> 00:08:43,809
the idea of function parameters and how
they used but there's a lot more to

108
00:08:43,809 --> 00:08:48,080
functions the basic concept is that you
write a block of code once you call it

109
00:08:48,080 --> 00:08:50,350
whenever you need to perform these
actions in your program that's the

110
00:08:50,350 --> 00:08:54,520
reason that you're creating functions and
this obviously saves a lot of typing but

111
00:08:54,520 --> 00:08:59,880
more importantly it makes the actual
code you create much easy to maintain so if

112
00:08:59,880 --> 00:09:03,740
you find yourself writing the same lines of
code repeatedly in a program that's a

113
00:09:03,740 --> 00:09:07,280
good indication that could you should
really consider putting that code in

114
00:09:07,280 --> 00:09:11,280
a function now functions can often
return results we mentioned that

115
00:09:11,280 --> 00:09:15,470
earlier in this video the face that all Python functions do

116
00:09:15,470 --> 00:09:20,650
return results and Python will ensure they
return none if you don't specify another value

117
00:09:20,650 --> 00:09:24,710
before we look at how to do that we
should really fix an error in center text

118
00:09:24,710 --> 00:09:30,070
so anyone used to printing text in Python would
probably expect center_text to

119
00:09:30,070 --> 00:09:30,680
behave

120
00:09:30,680 --> 00:09:35,630
similar to print but if we try and use
it to center a number we actually get an error

121
00:09:35,630 --> 00:09:42,660
so in other words if we come back here type....and we run it

122
00:09:42,660 --> 00:09:48,050
we actually get an error so the first two
lines print fine as you can see their spam

123
00:09:48,050 --> 00:09:53,370
and eggs and spam spam and eggs but when
the code processing got to line 16

124
00:09:53,370 --> 00:09:58,420
where we used a number as a argument we get an error
on the screen as you can see object

125
00:09:58,420 --> 00:10:02,510
of type int has no len now the good
thing here is that Python makes life easy

126
00:10:02,510 --> 00:10:06,470
but not forcing us to worry about
variable types most of the time but there

127
00:10:06,470 --> 00:10:11,520
are times when we need to be aware of
the type of the variable we are using now here

128
00:10:11,520 --> 00:10:15,630
when we get the length in characters and
you can see that's the code on line 9

129
00:10:15,630 --> 00:10:20,550
using len and texts which is the
variable in the parameter the argument

130
00:10:20,550 --> 00:10:24,340
we're assuming that its character length is actually
meaningful but the thing to remember

131
00:10:24,340 --> 00:10:29,000
here is the numbers don't have a character length though so  if that sounds odd consider

132
00:10:29,000 --> 00:10:34,130
that the string spam consist of 4 characters but we cannot really say that the

133
00:10:34,130 --> 00:10:39,150
number 12 consists of two characters
remember that in a computer 12 is stored

134
00:10:39,150 --> 00:10:44,670
....so basically in computer
terms 12 would be stored as the

135
00:10:44,670 --> 00:10:49,200
binary number 1100 this is what would
happen internally or in 16 bit notiation

136
00:10:49,200 --> 00:10:54,650
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:54,650 --> 00:10:59,770
differently so that's how a number would
be stored but in hexadecimal it will be

138
00:10:59,770 --> 00:11:04,880
stored as a C so looking at it in those
terms when we are not storing and using texts

139
00:11:04,880 --> 00:11:08,070
such what would really be meant by the
length if we're trying to get the length

140
00:11:08,070 --> 00:11:11,880
of the number doesn't really make sense as can see so we've used the print

141
00:11:11,880 --> 00:11:16,220
function to display numeric variables
and what happens when we do that is that

142
00:11:16,220 --> 00:11:19,720
Python automatically calls one of two
functions to produces a string

143
00:11:19,720 --> 00:11:22,000
representation of that variable

144
00:11:22,000 --> 00:11:27,350
the two functions are STR and REPR and
if string is defined for an

145
00:11:27,350 --> 00:11:31,020
object then the print function will use
that otherwise it is going to use this sa REPR

146
00:11:31,020 --> 00:11:35,250
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:35,250 --> 00:11:38,240
our own objects later in the course but for
now we can actually make a little bit of

148
00:11:38,240 --> 00:11:43,440
a change to make sure that this works
so lets just delete what I added their about binary numbers and

149
00:11:43,440 --> 00:11:46,680
go back to our method now what I want to do is
make a change

150
00:11:46,680 --> 00:11:55,880
here on line 9 so I want to type in.......and by doing that we should not need to

151
00:11:55,880 --> 00:12:01,070
change anything else and we should find the
code now works you can see how its

152
00:12:01,070 --> 00:12:06,460
now work and what's basically happened is
that by adding that code to line 9

153
00:12:06,460 --> 00:12:11,820
we are explicitly calling STR the STR function to
convert the argument that was passed to

154
00:12:11,820 --> 00:12:16,600
a string and we're doing that before we
attempt to get its length so consequently

155
00:12:16,600 --> 00:12:20,860
in the first two cases its already strings
so nothing else has done it still a string

156
00:12:20,860 --> 00:12:25,900
but in the third case where we passed the
parameters as an integer of the value 12 that

157
00:12:25,900 --> 00:12:31,060
gets converted to a string first time on line 9 so consequently on line 10 the len

158
00:12:31,060 --> 00:12:36,640
function is called is now a string that is
the argument that sent to the len function

159
00:12:36,640 --> 00:12:40,610
and that's why it works so we got now a
really useful function that can be used

160
00:12:40,610 --> 00:12:43,160
whenever we wanted to print text centered
on the screen

161
00:12:43,160 --> 00:12:46,400
this is a assuming and 80 character screen of course but it doesn't really act

162
00:12:46,400 --> 00:12:49,580
as a for replacement for the built in
print function if you remember from

163
00:12:49,580 --> 00:12:53,900
section 10 print can be used to write data to a file which is something center_text

164
00:12:53,900 --> 00:12:57,710
function clearly can't do
it at the moment because it's a useful way to

165
00:12:57,710 --> 00:13:02,810
introduce optional and default parameters what we're
going to do now is modify the function

166
00:13:02,810 --> 00:13:04,370
so that it could be used

167
00:13:04,370 --> 00:13:07,950
really pretty much as a direct
replacement for the print function and

168
00:13:07,950 --> 00:13:11,700
we also have a look at another approach
to the same problem now you already seen how

169
00:13:11,700 --> 00:13:16,040
can look at the Python source from IntelliJ by using to control click if your in a

170
00:13:16,040 --> 00:13:20,270
Linux machine or a Windows machine and on a
Mac you can do the same by holding down command

171
00:13:20,270 --> 00:13:21,670
and clicking it

172
00:13:21,670 --> 00:13:24,110
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:24,110 --> 00:13:29,350
command and click print so as you can see that's the

174
00:13:29,350 --> 00:13:32,850
definition for print so the first thing
to note is that you can actually view the

175
00:13:32,850 --> 00:13:37,720
real source of the built-in functions
the modules builtins.py

176
00:13:37,720 --> 00:13:41,550
stubs file and that what that means is that it
only contains descriptions of the

177
00:13:41,550 --> 00:13:45,450
functions without any actual
implementation so it's used by

178
00:13:45,450 --> 00:13:48,660
IntelliJ to provide the tool tips when you
were typing print for example showing

179
00:13:48,660 --> 00:13:52,060
you what arguments but the actual
implementation of the

180
00:13:52,060 --> 00:13:53,870
functions is actually written in C

181
00:13:53,870 --> 00:13:58,250
although we can't see all the source code
for the print function as such we can

182
00:13:58,250 --> 00:13:59,330
see its definition

183
00:13:59,330 --> 00:14:03,270
in a doc string describing the parameters
so you know its really still useful

184
00:14:03,270 --> 00:14:07,450
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:07,450 --> 00:14:11,170
and we are going to be looking at them a little
bit later but here everything between the

186
00:14:11,170 --> 00:14:17,820
two sets of triple quotes as you can see
from the line 477 through 486 is the doc

187
00:14:17,820 --> 00:14:23,490
string print so you can see the first
parameter looking at line 476 first

188
00:14:23,490 --> 00:14:28,160
parameters is called args and the fact that it's a plural rather
than arg gives an indication of what the

189
00:14:28,160 --> 00:14:32,240
asterix means so if our parameter is prefixed with a stab then

190
00:14:32,240 --> 00:14:36,880
Asterix and it represents a variable
number of arguments and what that really means is

191
00:14:36,880 --> 00:14:38,510
you can pass as many as you want

192
00:14:38,510 --> 00:14:42,070
separated by commas now seeing tha down a couple times in the course

193
00:14:42,070 --> 00:14:46,430
but we haven't really explained in detail so
here is an example what we mean so I'm going to

194
00:14:46,430 --> 00:14:57,650
close it down for now so type in.....then this prints the

195
00:14:57,650 --> 00:15:02,450
5 arguments so if we run that first second 3 4 spam so it prints the five

196
00:15:02,450 --> 00:15:05,710
arguments on the same line automatically
separating them with a space so what

197
00:15:05,710 --> 00:15:09,710
we could do is provide the
same functionality to our center

198
00:15:09,710 --> 00:15:13,670
text function quite easily and will start
doing that in the next video

