WEBVTT

0
00:00.290 --> 00:04.040
To get started, we're going to learn more about functions.

1
00:04.040 --> 00:13.040
And previously, we learned that functions are a really handy way of taking a complex set of instructions

2
00:13.040 --> 00:19.760
and packaging them together inside a block of code that has a name given to it.

3
00:19.760 --> 00:26.720
And when we need all of the lines of code that's packaged inside this function, all we have to do at

4
00:26.720 --> 00:34.160
any point later on in our code is just to call the function by typing its name, and then a set of parentheses.

5
00:34.490 --> 00:40.850
And when this line of code is run, it's going to search for where this function is defined, which is

6
00:40.850 --> 00:41.540
up here.

7
00:41.540 --> 00:45.770
And then it's going to execute all the lines of code that are contained within it.

8
00:46.490 --> 00:49.280
Now it's been a little while since we've seen functions.

9
00:49.280 --> 00:53.880
So to begin I want you to create a new function called greet(),

10
00:53.880 --> 00:59.430
and I want you to give it three lines of print() statements telling it to do three things.

11
00:59.430 --> 01:05.820
And finally, once you're done, go ahead and call the function so that it gets executed in the console.

12
01:05.820 --> 01:08.040
So pause the video and give that a go.

13
01:09.150 --> 01:09.660
All right.

14
01:09.660 --> 01:13.050
So to create a function we of course need the 'def' keyword,

15
01:13.050 --> 01:17.340
and we know it's a keyword because as soon as we type it it changes the color.

16
01:17.430 --> 01:21.720
And after the keyword, we get to give our function a name.

17
01:21.720 --> 01:24.090
So in my case it's called greet.

18
01:24.210 --> 01:29.610
Now after the greet, we add a set of parentheses after the name of the function.

19
01:29.940 --> 01:36.990
Finally, we add a colon and we enter into the block of code because we're indented over.

20
01:37.260 --> 01:42.270
Now, the first thing I'm going to do is to create a print statement that just says, "Hello".

21
01:42.270 --> 01:46.110
And then I'm going to say, well, "How do you do?"

22
01:46.230 --> 01:52.980
Now, nobody in England actually says this, but I always see it in the Learn English books, which

23
01:52.980 --> 01:53.880
is quite funny.

24
01:53.880 --> 01:55.770
I think people will give you very strange looks,

25
01:55.770 --> 01:58.650
if you go to somebody at the bakery and say, well, "How do you do?"

26
01:59.610 --> 02:08.320
But anyways, now finally we're going to add a final print statement, keeping in line with the Britishness

27
02:08.320 --> 02:12.670
of my function, I'm going to ask about the weather.

28
02:13.480 --> 02:19.480
Now, this line, on the other hand, is something that you will often hear British people say, they're

29
02:19.480 --> 02:23.620
obsessed about the weather, mostly because it's terrible all the time.

30
02:23.620 --> 02:29.410
So now that we've created our function, it's time to actually trigger the function or what we say in

31
02:29.410 --> 02:35.680
programming lingo, call the function, and we do that by calling the name of the function and then

32
02:35.680 --> 02:37.300
adding a set of parentheses.

33
02:37.300 --> 02:43.270
And now once the code reaches Line 6, it's going to search for this greet() function, which it'll find

34
02:43.270 --> 02:44.140
over here.

35
02:44.140 --> 02:48.070
And then it's going to carry out each of the lines of code one by one.

36
02:48.070 --> 02:53.350
So let's run the code and you should see each of the three print statements being called.

37
02:53.350 --> 02:55.120
So that's pretty simple.

38
02:55.810 --> 03:02.830
Notice that every single time I call the greet() function, it's going to do the same thing, right?

39
03:02.830 --> 03:05.800
It's just going to print out those three lines of code.

40
03:05.830 --> 03:12.730
Now in reality, it's very rare that you'll want to repeat the same instructions every single time

41
03:12.730 --> 03:14.080
when you call a function,

42
03:14.080 --> 03:20.590
it would be nice, wouldn't it? If we could modify some of the parts of the code inside the function

43
03:20.590 --> 03:23.650
and allow for a little bit of variation.

44
03:23.650 --> 03:30.070
For example, it'd be nice if we could greet somebody by their name so they could say, "Hello Angela," instead

45
03:30.070 --> 03:37.210
of just "Hello." "How do you do Angela?" So that it could change each time I call this function?

46
03:37.210 --> 03:41.620
Maybe next time it'll be, "How do you do Jack Bauer?" Et cetera.

47
03:41.620 --> 03:45.340
So how can we achieve this kind of functionality?

48
03:45.340 --> 03:49.540
Well, we need to look more closely at these parentheses.

49
03:49.570 --> 03:58.480
Now what we can do is we can actually add the name of a variable inside those parentheses to start giving

50
03:58.480 --> 04:00.370
our functions some inputs.

51
04:00.370 --> 04:06.140
So let's say that there's a variable called 'something' that's going to be passed to my_function.

52
04:06.140 --> 04:13.310
And this something can then be used inside this block of code, this function.

53
04:13.310 --> 04:15.320
And I could do something with it.

54
04:15.320 --> 04:22.340
Now in order to actually pass this value, when I call my_function, I have to add the data inside the

55
04:22.340 --> 04:23.120
parentheses.

56
04:23.120 --> 04:27.020
So let's say I decide to pass 123 over.

57
04:27.020 --> 04:33.380
Well, in this case, when this line of code gets triggered, the computer is going to search for where

58
04:33.380 --> 04:35.720
this function is declared, which is up here,

59
04:35.720 --> 04:44.000
and then it's going to pass over this piece of data 123, over to this variable called something.

60
04:44.000 --> 04:51.320
So now effectively inside this function called my_function, we now have a variable called something

61
04:51.320 --> 04:58.880
that's equal to 123, and that can then be used inside the block of code to do something with

62
04:58.880 --> 05:00.020
that piece of data.

63
05:01.100 --> 05:05.390
Now it's a bit like plugging a USB stick into a computer.

64
05:05.570 --> 05:12.500
If we took a different piece of input, we would end up with a different file being shown by the computer.

65
05:12.530 --> 05:18.980
So this means the computer can do something different depending on the input that we give it.

66
05:18.980 --> 05:23.390
And if we change that input, then it will receive a different piece of data.

67
05:23.960 --> 05:30.260
So in addition to a simple function, we can also create a function that allows for input.

68
05:30.740 --> 05:36.140
And to do that you saw earlier on the syntax is again the same as before,

69
05:36.140 --> 05:42.840
we use a 'def' to create the function and then let's just give it a slightly different name to differentiate

70
05:42.840 --> 05:48.900
it with the previous function greet_with_name() and then inside the parentheses,

71
05:48.900 --> 05:54.630
instead of leaving it blank, we get to create the name of the variable that's going to be passed over.

72
05:54.630 --> 05:59.820
So let's just call it 'name' because that describes the data that's going to be received.

73
05:59.820 --> 06:02.940
It's going to be the name of the person that I wish to greet.

74
06:02.940 --> 06:05.160
So now let's add a colon.

75
06:05.160 --> 06:08.280
And finally we can create our print statements.

76
06:08.490 --> 06:11.520
So I'm just going to copy these print statements from above.

77
06:11.520 --> 06:17.430
And now, instead of having these names hard-coded in here, I'm going to delete it,

78
06:17.430 --> 06:22.800
and I'm going to replace those names with the value of this variable.

79
06:22.800 --> 06:27.210
So I'm going to put it in using an f-string, like this.

80
06:27.210 --> 06:30.340
And then I'm going to do the same down here.

81
06:34.600 --> 06:39.670
When we actually call this function, it's also going to be a little bit different from previously on

82
06:39.670 --> 06:40.570
Line 6.

83
06:40.570 --> 06:44.260
So now, firstly the name of the function has changed.

84
06:44.260 --> 06:49.930
But notice how it's expecting some sort of input inside the parentheses,

85
06:49.930 --> 06:52.780
and it tells me that it wants to receive a name.

86
06:52.900 --> 06:58.810
It's when we call the function that we actually pass over the piece of data that's going to be stored

87
06:58.810 --> 07:00.880
inside this variable.

88
07:00.880 --> 07:03.520
Let's give it my own name, "Angela".

89
07:03.520 --> 07:09.550
And now when I hit Run, I want you to predict what's going to happen, what's actually going to be

90
07:09.550 --> 07:10.180
printed.

91
07:10.180 --> 07:14.110
So let's go ahead and comment out the previous line of code,

92
07:14.200 --> 07:16.660
and now run our code.

93
07:16.750 --> 07:19.850
And you should see that it says, "Hello Angela".

94
07:19.850 --> 07:20.930
"How do you do Angela?"

95
07:20.930 --> 07:25.610
So this piece of data has basically been inserted under this variable name,

96
07:25.610 --> 07:29.120
and then it's been put into both of these print statements.

97
07:29.120 --> 07:36.860
Now if I go ahead and call this function with a different piece of data, say a different name, then

98
07:36.860 --> 07:41.030
it's going to use that piece of data to modify this function

99
07:41.030 --> 07:43.750
so it does something a little bit different.

100
07:43.750 --> 07:49.000
So this way we can create one function that carries out some instructions,

101
07:49.000 --> 07:55.450
but every time we execute it, we get to modify it a little bit by changing the input.

102
07:56.170 --> 08:01.540
When we're talking about functions with inputs, there's two things that we're working with that's really

103
08:01.540 --> 08:03.370
important to differentiate.

104
08:03.850 --> 08:11.230
So we know that when we call this function and we pass over this piece of data, effectively we're creating

105
08:11.230 --> 08:13.660
a new variable called, something,

106
08:13.660 --> 08:17.770
and we're setting it to equal this piece of data that we're passing in.

107
08:17.980 --> 08:25.270
Now in programming lingo, you'll hear this being referred to as the Parameter, and this piece of data

108
08:25.270 --> 08:28.030
being referred to as the Argument.

109
08:28.600 --> 08:35.230
Now the argument is the actual piece of data that's going to be passed over to this function when it's

110
08:35.230 --> 08:39.970
being called, whereas the parameter is the name of that data,

111
08:39.970 --> 08:45.460
and we use the parameter inside the function to refer to it and to do things with it.

112
08:45.670 --> 08:51.280
Now, very often on the internet, when you come across somebody explaining something, be it on StackOverflow

113
08:51.280 --> 08:57.420
or in some piece of documentation, you'll hear them referring to these two types of things the parameter

114
08:57.420 --> 08:58.680
and the argument.

115
08:58.710 --> 09:04.230
So if you need to come back to this lesson and remind yourself the difference between those two things,

116
09:04.230 --> 09:07.590
the parameter is the name of the data that's being passed in,

117
09:07.590 --> 09:10.950
the argument is the actual value of the data.

118
09:11.310 --> 09:16.740
Now, sometimes people also get confused on the internet between these two words, but as long as you

119
09:16.740 --> 09:19.080
know what they mean, then you'll be in a good place.