1
00:00:05,160 --> 00:00:07,960
In this section, you've learnt
a lot about functions.

2
00:00:07,960 --> 00:00:11,620
Not everything yet, but enough to
let you write our own functions.

3
00:00:11,620 --> 00:00:16,790
You now understand what parameters and arguments
are, and how the arguments provide the values

4
00:00:16,790 --> 00:00:18,560
for your formal parameters.

5
00:00:18,560 --> 00:00:22,470
You define and name your parameters
when you define the function.

6
00:00:22,470 --> 00:00:24,760
You then pass arguments when
you call the function.

7
00:00:24,760 --> 00:00:30,710
You've seen how the flow of execution jumps
into the function body, when a function is

8
00:00:30,710 --> 00:00:34,980
called, and returns to the calling code, when
the function terminates.

9
00:00:34,980 --> 00:00:42,440
We covered scope, and saw that our parameter
variables only exist inside the function.

10
00:00:42,440 --> 00:00:44,940
They're not available in the
code outside the function.

11
00:00:44,940 --> 00:00:50,850
We still have a bit more to learn about scope,
but we know all we need to write our own functions.

12
00:00:50,850 --> 00:00:55,649
We looked at functions that perform actions,
and functions that return a result.

13
00:00:55,649 --> 00:01:00,840
It is possible for a function to do both,
but we haven't seen any examples of that.

14
00:01:00,840 --> 00:01:06,460
We deliberately didn't write functions that way,
because you should generally avoid doing it

15
00:01:06,460 --> 00:01:11,360
Functions should be self contained blocks
of code, that either perform some action,

16
00:01:11,360 --> 00:01:12,830
or return a result.

17
00:01:12,830 --> 00:01:17,660
Doing both in the same function can result
in code that's difficult to maintain.

18
00:01:17,660 --> 00:01:21,800
Functions that perform an action used to be
called procedures, but modern programming

19
00:01:21,800 --> 00:01:23,490
languages no longer make that distinction.

20
00:01:23,490 --> 00:01:26,890
Instead, they return None, in Python.

21
00:01:26,890 --> 00:01:32,640
Languages like C and Java use void as the
return type, to indicate that nothing is returned.

22
00:01:32,640 --> 00:01:38,360
We've seen how to provide default
values for our parameters.

23
00:01:38,360 --> 00:01:43,080
Default values make using the function easier,
because you don't have to provide arguments

24
00:01:43,080 --> 00:01:46,060
for commonly used parameter values.

25
00:01:46,060 --> 00:01:51,130
We used the parameter name, when specifying
an argument, in the banner_text function.

26
00:01:51,130 --> 00:01:56,800
Keyword arguments let you pass arguments in
a different order than the parameters are defined.

27
00:01:56,800 --> 00:02:01,130
More importantly, they allow arguments to
be provided, when a parameter with a default

28
00:02:01,130 --> 00:02:03,670
value appears earlier in the list.

29
00:02:03,670 --> 00:02:10,720
We saw one example of that, when we wanted to change
the screen_width without providing any text.

30
00:02:10,720 --> 00:02:14,050
Docstrings are an important
feature of Python functions.

31
00:02:14,050 --> 00:02:18,190
They're used to document what the
function does, and how to use it.

32
00:02:18,190 --> 00:02:22,220
You've had a lot of practice writing Docstrings,
and will get more as you write more functions

33
00:02:22,220 --> 00:02:24,360
in later sections.

34
00:02:24,360 --> 00:02:27,430
Function annotations also help
with documenting your functions.

35
00:02:27,430 --> 00:02:31,200
They describe the type of the
arguments, and the return value.

36
00:02:31,200 --> 00:02:36,850
Type hints perform a similar job, but are
used for variables in your main program.

37
00:02:36,850 --> 00:02:40,980
A very important part of programming is testing.

38
00:02:40,980 --> 00:02:46,100
We learned how to activate a virtual environment,
so that we could run our code like our users will run it.

39
00:02:46,100 --> 00:02:49,180
We also used a function
to test the HiLo game.

40
00:02:49,180 --> 00:02:53,790
Putting the code into a function allowed
us to test every possible value.

41
00:02:53,790 --> 00:02:56,090
That would be very tedious to do manually.

42
00:02:56,090 --> 00:03:01,430
We haven't looked at automated testing yet,
but that example demonstrated how useful it can be.

43
00:03:01,430 --> 00:03:05,900
If we can get the computer to test our code,
we can perform more thorough tests, and save

44
00:03:05,900 --> 00:03:08,350
ourselves a lot of time.

45
00:03:08,350 --> 00:03:13,420
We finished the section by looking at
the 5 different types of parameters.

46
00:03:13,420 --> 00:03:18,100
We haven't discussed positional-only parameters,
because you're unlikely to use them.

47
00:03:18,100 --> 00:03:22,760
var-keyword parameters will be discussed later,
after you've learnt about Python dictionaries.

48
00:03:22,760 --> 00:03:27,830
We've seen examples of the other 3 parameter
types, and used them in our functions.

49
00:03:27,830 --> 00:03:32,650
In the next section, we'll look at Python
dictionaries and sets.

