1
00:00:04,730 --> 00:00:09,110
In this video, we'll make an improvement
to our banner text function. In the

2
00:00:09,110 --> 00:00:13,960
process, we'll also learn about keyword
arguments. I'll start with the change.

3
00:00:13,960 --> 00:00:18,680
At the moment, our function isn't very
intuitive. Looking at line 19, how would

4
00:00:18,680 --> 00:00:22,190
you know that line will print a blank
row within the banner. You could read the

5
00:00:22,190 --> 00:00:25,730
code for the function, of course, but you
wouldn't like to read all the code for

6
00:00:25,730 --> 00:00:30,380
every function you use, before you could
use it. You'll often write functions that

7
00:00:30,380 --> 00:00:34,129
other members of your team will use.
They're not going to be happy if they

8
00:00:34,129 --> 00:00:38,030
have to read through the source code
either. The obvious thing to do, is to

9
00:00:38,030 --> 00:00:42,070
write documentation for the function.
That's something we'll be doing soon.

10
00:00:42,070 --> 00:00:46,940
Documentation is essential, but you can
also make your functions easier to use,

11
00:00:46,940 --> 00:00:51,620
by making them more intuitive. Calling
the built in print function without any

12
00:00:51,620 --> 00:00:56,239
arguments, prints a blank line. Now that's
sort of what we're doing here, so I think

13
00:00:56,239 --> 00:01:00,830
that calling banner texts without any
arguments make sense. So let's make a

14
00:01:00,830 --> 00:01:08,270
change to line 19. We're going to remove
the arguments, so that it's just got

15
00:01:08,270 --> 00:01:13,290
the left and right parentheses there. Alright,
by doing that we've now got an error,

16
00:01:13,290 --> 00:01:18,220
and that's fine - you'll often create errors 
when making changes to your code.

17
00:01:18,220 --> 00:01:21,460
In this case, the error's because we're
not passing any text to the function,

18
00:01:21,460 --> 00:01:27,060
and it requires some text. To fix that, we 
can provide a default value for the text

19
00:01:27,060 --> 00:01:31,950
parameter. Let's come up here and do that.
We're going to put an equals and a space

20
00:01:31,950 --> 00:01:37,350
in double quotes. That's our default value
for the text parameter. And if we have a

21
00:01:37,350 --> 00:01:40,830
look at line 19, you can see that's now
removed the error. We know that printing

22
00:01:40,830 --> 00:01:44,729
a space does what we want, so we can make
that space that the default value for

23
00:01:44,729 --> 00:01:49,400
the text parameter. So if we run the program,

24
00:01:51,020 --> 00:01:55,250
and we still get a neatly formatted
banner, just like we had before. So why

25
00:01:55,250 --> 00:01:58,700
have I bothered doing this? Well the main
reason is that I think it makes the

26
00:01:58,700 --> 00:02:04,280
function more intuitive. You don't have
to remember to use a space, when you want

27
00:02:04,280 --> 00:02:07,850
to include a blank line in the banner.
But I've also done it so that I could

28
00:02:07,850 --> 00:02:12,440
show keyword arguments being used. We
have to use them here, if we want to

29
00:02:12,440 --> 00:02:15,739
change the width of our banner. So I'm
going to change it now to print in a

30
00:02:15,739 --> 00:02:20,060
width of 60 characters. That's easy - we
just pass 60. That's the screen width

31
00:02:20,060 --> 00:02:23,630
argument on each line. So I'm going to
use IntelliJ's search and replace feature

32
00:02:23,630 --> 00:02:29,120
here, so I'm gonna click on Edit, click on
Find, click on Replace. So we're

33
00:02:29,120 --> 00:02:33,920
going to search for double quote and a
right parenthesis, and we're going to

34
00:02:33,920 --> 00:02:41,760
replace that on the next field, with double
quote comma space 60 and a right parenthesis.

35
00:02:41,760 --> 00:02:44,660
Click on over here and Replace all.

36
00:02:44,670 --> 00:02:48,000
You can now see that we've added
the argument to most of the function

37
00:02:48,000 --> 00:02:53,040
calls, and what I'll do is, I'll add the
argument to line 19 manually. So here, I'm

38
00:02:53,040 --> 00:02:58,140
just going to add a comma 60, and you can
see by doing that, we've now got a

39
00:02:58,140 --> 00:03:03,420
problem on line 19. So we want to provide
a value for screen width, but we also

40
00:03:03,420 --> 00:03:07,110
want to use the default value for the text
argument. Now, obviously, we can't just

41
00:03:07,110 --> 00:03:11,040
leave the text argument blank - that gives
us an error. Now this is why keyword

42
00:03:11,040 --> 00:03:16,470
arguments are useful. We can specify
which parameter we want that 60 argument

43
00:03:16,470 --> 00:03:22,260
to be used for, by naming it. So I'm going
to come up here - line 19, change that - delete

44
00:03:22,260 --> 00:03:26,790
the comma and change that to screen
underscore width equals 60, as you can

45
00:03:26,790 --> 00:03:31,140
see there. Just like with the parameter
default values, we don't put a space

46
00:03:31,140 --> 00:03:34,500
either side of the equals, and you can
see that's fixed the error.

47
00:03:34,500 --> 00:03:37,460
If I run the program now,

48
00:03:38,460 --> 00:03:42,780
you can see that it still works. In the
last few videos, we've used our two

49
00:03:42,780 --> 00:03:46,680
parameters with both positional
arguments and keyword arguments. That's

50
00:03:46,680 --> 00:03:51,330
the default parameter type in Python.
I mentioned that briefly in an earlier

51
00:03:51,330 --> 00:03:55,830
slide, when we introduced the idea of
positional arguments. Let's have a quick

52
00:03:55,830 --> 00:04:00,180
look at some documentation. The language
reference is a bit heavy for this early

53
00:04:00,180 --> 00:04:05,540
in the course, so what I'm going to do is
go to an entry in the Python glossary.

54
00:04:07,540 --> 00:04:12,040
Up until now, we've used the first type
of parameters - positional or keyword

55
00:04:12,050 --> 00:04:16,540
parameters. They're the default and you
can pass their arguments, either by

56
00:04:16,548 --> 00:04:20,660
position or using their name, as we've
just done with the screen_width parameter

57
00:04:20,660 --> 00:04:25,520
in our code. Positional or keyword
arguments are the only type we'll be using in

58
00:04:25,520 --> 00:04:29,240
this section, and in fact, for most of the
course. Now I'm going to be covering

59
00:04:29,240 --> 00:04:34,240
these last two, later - that's the
var-positional and var-keyword types

60
00:04:34,240 --> 00:04:39,520
in the list. The remaining
two are actually quite advanced. In fact,

61
00:04:39,520 --> 00:04:44,540
positional-only parameters only became
part of the language in Python 3.8.

62
00:04:44,540 --> 00:04:48,340
Check out PEP 570 if you want to read
about why they're added and why you

63
00:04:48,350 --> 00:04:52,370
might want to use them. But as I said,
they are quite advanced. You wouldn't use

64
00:04:52,370 --> 00:04:55,430
them unless you were writing your own
API, and by that time all this

65
00:04:55,430 --> 00:04:58,810
documentation will make sense.

66
00:06:17,720 --> 00:06:20,800
See you in the next video.

