1
00:00:00,490 --> 00:00:03,850
To get started we're going to learn more about functions.

2
00:00:04,330 --> 00:00:09,330
And previously we learned that functions are a really handy way of taking a

3
00:00:10,540 --> 00:00:15,540
complex set of instructions and packaging them together inside a block of code

4
00:00:17,470 --> 00:00:19,540
that has a name given to it.

5
00:00:20,080 --> 00:00:24,580
And when we need all of the lines of code that's packaged inside this

6
00:00:24,580 --> 00:00:25,390
function,

7
00:00:25,390 --> 00:00:30,390
all we have to do at any point later on in our code is just to call the function

8
00:00:30,940 --> 00:00:34,030
by typing its name and then a set of parentheses.

9
00:00:34,750 --> 00:00:36,760
And when this line of code is run,

10
00:00:37,030 --> 00:00:41,440
it's going to search for where this function is defined, which is up here,

11
00:00:41,830 --> 00:00:45,490
and then it's going to execute all the lines of code that are contained within

12
00:00:45,490 --> 00:00:49,330
it. Now it's been a little while since we've seen functions.

13
00:00:49,390 --> 00:00:54,390
So I want you to go ahead and find the starting replit for day 8

14
00:00:55,030 --> 00:00:56,470
which is at this URL

15
00:00:56,680 --> 00:01:01,270
and then fork it to create your own copy. To begin,

16
00:01:01,330 --> 00:01:04,569
I want you to create a new function called greet,

17
00:01:05,110 --> 00:01:08,110
and I want you to give it three lines of print statements,

18
00:01:08,350 --> 00:01:12,220
telling it to do three things. And finally, once you're done,

19
00:01:12,250 --> 00:01:16,630
go ahead and call the function so that it gets executed in the console.

20
00:01:16,900 --> 00:01:20,560
So pause the video and give that a go. All right.

21
00:01:20,560 --> 00:01:23,770
So to create a function, we of course need a def keyword.

22
00:01:24,130 --> 00:01:26,710
And we know it's a keyword, because as soon as we type it,

23
00:01:26,770 --> 00:01:30,790
it changes to the blue color. And after the key word,

24
00:01:30,850 --> 00:01:35,170
we get to give our function a name. So in my case, it's called greet.

25
00:01:35,860 --> 00:01:40,630
Now after the greet, we add a set of parentheses after the name of the function.

26
00:01:41,530 --> 00:01:42,363
Finally,

27
00:01:42,430 --> 00:01:47,430
we add a colon and we enter into the block of code because we're indented over.

28
00:01:48,880 --> 00:01:49,390
Now,

29
00:01:49,390 --> 00:01:52,840
the first thing I'm going to do is to create a print statement that just says

30
00:01:52,840 --> 00:01:55,720
Hello. And then I'm going to say, well,

31
00:01:56,200 --> 00:02:00,460
how do you do? Nobody in England actually says this,

32
00:02:00,790 --> 00:02:04,990
but I always see it in the learn English books, which is quite funny.

33
00:02:05,440 --> 00:02:07,120
I think people will give you a very strange look

34
00:02:07,150 --> 00:02:11,980
if you go to somebody at the bakery and say, well, how do you do? But anyways,

35
00:02:12,880 --> 00:02:16,600
now finally, we're going to add a final print statement,

36
00:02:17,050 --> 00:02:21,670
keeping in line with the Britishness of my function,

37
00:02:22,090 --> 00:02:23,710
going to ask about the weather.

38
00:02:24,940 --> 00:02:29,770
Now this line, on the other hand, is something that you will often hear a British

39
00:02:29,770 --> 00:02:32,080
people say. They're obsessed about the weather,

40
00:02:32,350 --> 00:02:34,750
mostly because it's terrible all the time.

41
00:02:35,500 --> 00:02:37,930
So now that we've created our function,

42
00:02:37,930 --> 00:02:42,100
it's time to actually trigger the function or what we see in programming lingo,

43
00:02:42,400 --> 00:02:43,540
call the function.

44
00:02:43,900 --> 00:02:48,100
And we do that by calling the name of the function and then adding a set of

45
00:02:48,100 --> 00:02:51,940
parentheses. And now once the code reaches line six

46
00:02:51,970 --> 00:02:55,750
it's going to search for this greet function, which it'll find over here

47
00:02:56,050 --> 00:02:59,590
and then it's going to carry out each of the lines of code one by one.

48
00:03:00,040 --> 00:03:04,000
So let's run the code and you should see each of the three print statements

49
00:03:04,060 --> 00:03:06,730
being called. So that's pretty simple.

50
00:03:08,470 --> 00:03:12,100
Notice that every single time I call the greet function,

51
00:03:12,640 --> 00:03:15,190
it's going to do the same thing, right?

52
00:03:15,190 --> 00:03:19,630
It's just going to print out those three lines of code. Now, in reality,

53
00:03:19,630 --> 00:03:24,630
it's very rare that you'll want to repeat the same instructions every single

54
00:03:24,790 --> 00:03:28,090
time when you call a function. It would be nice, wouldn't it,

55
00:03:28,110 --> 00:03:33,110
if we could modify some of the parts of the code inside the function and allow

56
00:03:34,030 --> 00:03:36,790
for a little bit of variation. For example,

57
00:03:36,790 --> 00:03:40,600
it would be nice if we could greet somebody by the name so that it could say,

58
00:03:40,900 --> 00:03:45,580
hello Angela, instead of just, hello. How do you do Angela?

59
00:03:45,910 --> 00:03:49,480
So that it could change each time I call this function.

60
00:03:49,570 --> 00:03:53,860
Maybe next time it will be, how do you do Jack Bauer, et cetera.

61
00:03:54,130 --> 00:03:58,390
So how can we achieve this kind of functionality? Well,

62
00:03:58,390 --> 00:04:01,600
we need to look more closely at these parentheses.

63
00:04:02,200 --> 00:04:07,200
Now what we can do is we can actually add the name of a variable inside those

64
00:04:08,800 --> 00:04:12,520
parentheses to start giving our functions some inputs.

65
00:04:12,970 --> 00:04:17,079
So let's say that there's a variable called something that's going to be passed

66
00:04:17,110 --> 00:04:22,110
to my function. And this something can then be used inside this block of code,

67
00:04:24,610 --> 00:04:25,443
this function.

68
00:04:25,900 --> 00:04:30,520
And I could do something with it. Now in order to actually pass this value

69
00:04:30,520 --> 00:04:35,380
when I call my function, I have to add the data inside the parentheses.

70
00:04:35,650 --> 00:04:39,910
So let's say I decide to pass 123 over, well

71
00:04:39,910 --> 00:04:43,120
in this case, when this line of code gets triggered,

72
00:04:43,570 --> 00:04:46,990
the computer is going to search for where this function is declared,

73
00:04:47,020 --> 00:04:51,940
which is up here, and then it's going to pass over this piece of data,

74
00:04:52,300 --> 00:04:56,080
123, over to this variable called something.

75
00:04:56,620 --> 00:05:00,940
So now, effectively, inside this function called my_function,

76
00:05:01,240 --> 00:05:05,680
we now have a variable called something that's equal to 123

77
00:05:06,400 --> 00:05:11,400
and that can then be used inside the block of code to do something with that

78
00:05:11,410 --> 00:05:12,243
piece of data.

79
00:05:14,470 --> 00:05:18,220
Now it's a bit like plugging in a USB stick into a computer.

80
00:05:18,790 --> 00:05:21,100
If we took a different piece of input,

81
00:05:21,190 --> 00:05:25,450
we would end up with a different file being shown by the computer.

82
00:05:25,900 --> 00:05:30,900
So this means the computer can do something different depending on the input that

83
00:05:31,390 --> 00:05:33,880
we give it. And if we change that input,

84
00:05:34,000 --> 00:05:36,280
then it will receive a different piece of data.

85
00:05:37,180 --> 00:05:39,700
So in addition to a simple function,

86
00:05:39,730 --> 00:05:43,060
we can also create a function that allows for input.

87
00:05:44,140 --> 00:05:49,140
And to do that you saw earlier on the syntax is again the same as before we use

88
00:05:49,690 --> 00:05:51,940
a Def to create the function.

89
00:05:52,360 --> 00:05:56,230
And then let's just give it a slightly different name to differentiate it with

90
00:05:56,230 --> 00:05:59,150
the previous function, greet_with_name.

91
00:05:59,720 --> 00:06:03,650
And then inside the parentheses, instead of leaving it blank,

92
00:06:03,890 --> 00:06:07,550
we get to create the name of the variable that's going to be passed over.

93
00:06:08,000 --> 00:06:12,170
So let's just call it name because that describes the data that's going to be

94
00:06:12,170 --> 00:06:15,770
received. It's going to be the name of the person that I wish to greet.

95
00:06:16,340 --> 00:06:21,140
So now let's add a colon and finally we can create our print statements.

96
00:06:21,740 --> 00:06:25,640
So I'm just going to copy these print statements from above and now,

97
00:06:25,670 --> 00:06:30,530
instead of having these names hard coded in here, I'm going to delete it

98
00:06:30,680 --> 00:06:35,680
and I'm going to replace those names with the value of this variable.

99
00:06:36,110 --> 00:06:40,130
So I'm going to put it in using an fstring like this,

100
00:06:40,580 --> 00:06:43,250
and then I'm going to do the same down here.

101
00:06:46,740 --> 00:06:47,573
Right?

102
00:06:47,910 --> 00:06:49,740
When we actually call this function,

103
00:06:49,770 --> 00:06:53,430
it's also going to be a little bit different from previously on line six.

104
00:06:53,970 --> 00:06:57,180
So now firstly, the name of the function has changed.

105
00:06:57,630 --> 00:07:02,630
But notice how it's expecting some sort of input inside the parentheses.

106
00:07:03,120 --> 00:07:05,640
And it tells me that it wants to receive a name.

107
00:07:06,180 --> 00:07:10,830
It's when we call the function that we actually pass over the piece of data

108
00:07:10,860 --> 00:07:15,570
that's going to be stored inside this variable. Let's give it my own name,

109
00:07:15,900 --> 00:07:19,110
Angela. And now when I hit run,

110
00:07:19,470 --> 00:07:21,420
I want you to predict what's going to happen,

111
00:07:21,450 --> 00:07:23,190
what's actually going to be printed.

112
00:07:23,400 --> 00:07:28,400
So let's go ahead and comment out the previous line of code and now run our code.

113
00:07:30,240 --> 00:07:34,020
And you should see that it says, hello Angela, how do you do Angela.?

114
00:07:34,020 --> 00:07:38,610
So this piece of data has basically been inserted under this variable name

115
00:07:38,910 --> 00:07:42,810
and then it's been put into both of these print statements. Now,

116
00:07:42,810 --> 00:07:47,100
if I go ahead and call this function with a different piece of data,

117
00:07:47,760 --> 00:07:49,230
say a different name,

118
00:07:49,650 --> 00:07:54,060
then it's going to use that piece of data to modify this function

119
00:07:54,300 --> 00:07:56,580
so it does something a little bit different.

120
00:07:57,090 --> 00:08:02,010
So this way we can create one function that carries out some instructions,

121
00:08:02,310 --> 00:08:06,240
but every time we execute it, it gets a modify it a little bit

122
00:08:06,510 --> 00:08:08,340
by changing the inputs.

123
00:08:09,360 --> 00:08:11,760
When we're talking about functions with inputs,

124
00:08:12,030 --> 00:08:13,950
there's two things that we're working with

125
00:08:13,980 --> 00:08:16,200
that's really important to differentiate.

126
00:08:17,190 --> 00:08:22,170
So we know that when we call this function and we pass over this piece of data,

127
00:08:22,500 --> 00:08:23,460
effectively

128
00:08:23,490 --> 00:08:28,490
we're creating a new variable called something and we're setting it to equal this

129
00:08:28,860 --> 00:08:33,030
piece of data that we're passing it. Now in programming lingo,

130
00:08:33,059 --> 00:08:38,059
you'll hear this being referred to as the parameter and this piece of data

131
00:08:38,460 --> 00:08:42,240
being referred to as the argument. Now,

132
00:08:42,240 --> 00:08:45,000
the argument is the actual piece of data

133
00:08:45,030 --> 00:08:49,110
that's going to be passed over to this function when it's being called.

134
00:08:49,560 --> 00:08:52,920
Whereas the parameter is the name of that data

135
00:08:53,340 --> 00:08:57,990
and we use the parameter inside the function to refer to it and to do things

136
00:08:57,990 --> 00:09:01,620
with it. Now very often on the internet when you come across

137
00:09:01,620 --> 00:09:03,090
somebody explaining something

138
00:09:03,090 --> 00:09:06,060
be it on Stack Overflow or in some piece of documentation,

139
00:09:06,420 --> 00:09:09,600
you'll hear them referring to these two types of things,

140
00:09:09,600 --> 00:09:11,550
the parameter and the argument.

141
00:09:12,090 --> 00:09:15,930
So if you need to, come back to this lesson and remind yourself the difference

142
00:09:15,930 --> 00:09:17,160
between those two things.

143
00:09:17,550 --> 00:09:20,550
The parameter is the name of the data that's being passed in,

144
00:09:20,850 --> 00:09:24,990
the argument is the actual value of the data. Now,

145
00:09:25,020 --> 00:09:28,770
sometimes people also get confused on the internet between these two words,

146
00:09:29,040 --> 00:09:32,010
but as long as you know what they means, then you'll be in a good place.

