1
00:00:04,840 --> 00:00:08,980
So I've created a new project in
IntelliJ for this section, as I've been

2
00:00:09,020 --> 00:00:12,700
doing for each section of the course.
 I've called this one, as you can see,

3
00:00:12,700 --> 00:00:17,780
Functions_Intro. So to get started, 
we need a new python file for our code.

4
00:00:17,780 --> 00:00:21,240
I'm going to call mine functions.

5
00:00:27,220 --> 00:00:31,030
Okay, so we've used some functions
already in the course. In this video,

6
00:00:31,030 --> 00:00:35,400
we'll start writing our own. Once you've
seen a couple of examples, I'll discuss

7
00:00:35,400 --> 00:00:40,560
the formal definition of a function. A
Python function definition starts with

8
00:00:40,570 --> 00:00:44,840
the keyword def. I'm going to call this
one multiply, so I'm going to type def

9
00:00:44,840 --> 00:00:52,760
space multiply. Then I'm going to add left
and right parentheses to that, and add a colon.

10
00:00:52,760 --> 00:00:56,360
Function names follow the same
rules as variable names. They're written

11
00:00:56,379 --> 00:01:00,640
in lowercase and mustn't start with a
digit. You can use an underscore

12
00:01:00,640 --> 00:01:05,290
to separate the words in the name, just
like we did for our variables. After the

13
00:01:05,290 --> 00:01:09,580
function name, we have opening and
closing parentheses. Finally, we end with

14
00:01:09,580 --> 00:01:12,800
a colon. Our function starts a new block of code,

15
00:01:12,800 --> 00:01:17,620
then any statement that starts a new
block, always ends with a colon. Inside

16
00:01:17,620 --> 00:01:21,440
the function, we write the code that we
want to be executed, when the function

17
00:01:21,440 --> 00:01:27,520
gets called. I'll keep this one simple to
start with. We're just going to multiply two numbers:

18
00:01:27,540 --> 00:01:34,260
result is equal to 10.5 x 4. Press ENTER and then I'm

19
00:01:34,260 --> 00:01:42,280
going to type return result. So line 2
is simple - it just binds the variable result

20
00:01:42,280 --> 00:01:45,860
to the result of multiplying
the two numbers. Our multiply function

21
00:01:45,860 --> 00:01:50,680
multiplies 10.5 by 4. Now that's not
very useful if the function doesn't let

22
00:01:50,680 --> 00:01:54,550
us know what the answer is. So that we
can find out what value our function

23
00:01:54,550 --> 00:01:58,930
calculated, we tell the function to
return the result. So that's the return

24
00:01:58,930 --> 00:02:02,680
statement on line three. You've probably got a few

25
00:02:02,680 --> 00:02:05,860
questions at this point, but don't worry,
we're just starting, and we will be

26
00:02:05,860 --> 00:02:09,639
covering more about functions in a
moment. First though, let's see how to use

27
00:02:09,639 --> 00:02:12,120
the function. You already know how to do that,

28
00:02:12,120 --> 00:02:15,640
because we've used quite a few
functions in the course already. The only

29
00:02:15,640 --> 00:02:19,900
difference is, this time we wrote the
function ourselves. So I'm going to press

30
00:02:19,900 --> 00:02:23,740
ENTER a few times. Come down to line 6 -
I'm going to type answer

31
00:02:23,740 --> 00:02:29,900
is equal to multiply, noting that IntelliJ
 helps us automatically, by giving us that.

32
00:02:29,900 --> 00:02:33,040
Press ENTER there. On the next
line I'm going to print, and in

33
00:02:33,040 --> 00:02:38,659
parentheses, answer.
So on line six, I'm calling the multiply

34
00:02:38,659 --> 00:02:42,440
function. It doesn't have any arguments,
but we still need to include the

35
00:02:42,440 --> 00:02:46,879
parentheses, as I've done, and because our
function returns a value, we can bind the

36
00:02:46,879 --> 00:02:52,190
variable answer to the return value. And
by doing that, that lets us print it out

37
00:02:52,190 --> 00:02:57,040
on line seven. We run the program now,

38
00:02:58,120 --> 00:03:03,040
and we can see that the answer is 42,
well actually, 42.0 to be precise.

39
00:03:03,040 --> 00:03:08,680
We multiplied an int by a float, so the result
is a float. Now that's a very simple

40
00:03:08,690 --> 00:03:13,100
function, and it's not really doing
anything useful. We'll change it soon, so

41
00:03:13,100 --> 00:03:17,210
that it does in fact, do something more
useful. Before we do that, though, let's

42
00:03:17,210 --> 00:03:23,120
examine the structure of our function. It
starts with the Python keyword def - def

43
00:03:23,120 --> 00:03:27,560
is short for define. That's followed by
the name of the function - multiply, in

44
00:03:27,560 --> 00:03:32,900
this case. All function definitions have
parentheses after the function name. You

45
00:03:32,900 --> 00:03:36,620
can also define parameters inside the
parentheses, but at the moment, our

46
00:03:36,620 --> 00:03:41,540
function doesn't have any parameters. A
function definition starts a new block,

47
00:03:41,540 --> 00:03:46,040
which means that we have to indent the
body of the function. Lines two and three

48
00:03:46,040 --> 00:03:50,150
are indented one level, which makes them
part of the function, and also notice

49
00:03:50,150 --> 00:03:54,740
that the function definition ends with a
colon, at the end of line one.

50
00:03:54,740 --> 00:03:59,840
Our multiply function is performing a
calculation. We return the answer to the

51
00:03:59,840 --> 00:04:05,090
calling code, using the return keyword
that's on line three. The convention is

52
00:04:05,090 --> 00:04:09,890
to have two blank lines after a function.
Now that's also mentioned in the Python

53
00:04:09,890 --> 00:04:15,290
style guide - pep 8 - and that's why our
main code starts on line six, so that two

54
00:04:15,290 --> 00:04:20,089
blank lines separate it from the
function. It makes your code much easier

55
00:04:20,089 --> 00:04:24,260
to read, especially once you start having
several functions. Make sure you leave

56
00:04:24,260 --> 00:04:30,170
two black lines, before and after your
functions. Our main code calls the

57
00:04:30,170 --> 00:04:35,120
function on line six. To call a function,
you just use its name, with any arguments

58
00:04:35,120 --> 00:04:38,540
in parentheses. You've seen a lot of
functions being called so far, so that's

59
00:04:38,540 --> 00:04:44,420
nothing new. Because our function returns
a value, we bind the variable answer to

60
00:04:44,420 --> 00:04:49,590
the value that the function returns.
If you're wondering why we can't just

61
00:04:49,590 --> 00:04:54,200
use the variable result from line two,
I'll be talking about that very soon.

62
00:04:54,200 --> 00:04:57,360
Keep watching, because there are still a
few more things that we need to know

63
00:04:57,360 --> 00:05:02,480
about functions. Alright, so that's the
basic structure of a Python function.

64
00:05:02,480 --> 00:05:06,860
Let's see some of that on slides before
I finish the video.

65
00:05:06,860 --> 00:05:10,800
So you can see that the function
definition starts with the keyword def.

66
00:05:10,800 --> 00:05:16,419
Next we have the name of the function. If
the function will take parameters, they're

67
00:05:16,420 --> 00:05:20,780
declared in parentheses. And keep in
mind that the parentheses are required,

68
00:05:20,780 --> 00:05:25,090
even when there are no parameters.
Alright, so in the next video, we'll have

69
00:05:25,090 --> 00:05:28,509
a look at what happens when our code
calls a function. We'll step through the

70
00:05:28,509 --> 00:05:32,900
code in the debugger, to see how a
function call alters the flow of execution.

71
00:05:32,900 --> 00:05:36,100
See you in the next video.

