1
00:00:00,090 --> 00:00:06,870
So we know a crash course we have encountered already several functions that are built in either Python

2
00:00:06,870 --> 00:00:07,980
or Nampai.

3
00:00:08,760 --> 00:00:11,970
And here I want to show you how you can define your own functions.

4
00:00:12,660 --> 00:00:20,040
So to be honest, using functions is never really necessary, but it is often very, very helpful to

5
00:00:20,040 --> 00:00:26,730
structure your code to make it easier to understand what's going on and just to make the code much shorter,

6
00:00:26,880 --> 00:00:30,240
especially if you try to do the same thing over and over.

7
00:00:30,570 --> 00:00:32,729
It's a very good idea to define a function.

8
00:00:33,480 --> 00:00:39,660
So as an example, we will later on define our own dot product function so that this would have been

9
00:00:39,660 --> 00:00:44,760
very helpful if this function would not have been implemented already into Nampai.

10
00:00:45,570 --> 00:00:49,470
So, but first of all, I want to show you in general how a function works.

11
00:00:50,370 --> 00:00:54,000
So the main command for defining a function is deaf.

12
00:00:54,570 --> 00:00:57,570
Then you write a name, you can choose whatever name you want.

13
00:00:58,020 --> 00:01:02,310
And then in the brackets, you can provide arguments.

14
00:01:02,580 --> 00:01:05,850
So I say you can because this is not really necessary.

15
00:01:06,390 --> 00:01:10,800
And in our first example, I will define a function without any arguments.

16
00:01:11,430 --> 00:01:16,260
And so I write death and I give it a name and the name will be say hello.

17
00:01:17,550 --> 00:01:20,440
And as I said, it will not give any arguments.

18
00:01:20,460 --> 00:01:26,790
So just write the brackets open and close and then we can go ahead with the actual commands.

19
00:01:27,570 --> 00:01:34,560
And the only thing that I want this function to do is just print something like, Hello.

20
00:01:36,120 --> 00:01:39,000
OK, so we run this and nothing happens.

21
00:01:39,210 --> 00:01:41,700
This is because we have just defined the function.

22
00:01:41,940 --> 00:01:47,520
We did not call the function yet to call the function, we just write, say hello.

23
00:01:48,330 --> 00:01:53,880
So if I don't write the brackets, it will not work because we need the brackets, but we don't need

24
00:01:53,880 --> 00:01:54,750
any arguments.

25
00:01:55,380 --> 00:02:00,180
So I write say hello brackets and then the output will be hello, as we have defined.

26
00:02:01,140 --> 00:02:04,530
So let's see what happens if I provide some argument.

27
00:02:06,060 --> 00:02:11,880
There will be an error because there are zero positional arguments, but one argument was given.

28
00:02:12,510 --> 00:02:15,960
So it just works, really if you don't provide any arguments in this case.

29
00:02:17,930 --> 00:02:23,810
So the next thing that we are going to do is we are going to create our own dot product function.

30
00:02:24,680 --> 00:02:31,460
So let me redefine the two vectors from before, so we had an array.

31
00:02:31,760 --> 00:02:35,000
So this is a good exercise for you to remember how to define an array.

32
00:02:35,450 --> 00:02:42,860
And we choose just some random numbers and another vector, a vector to with some other numbers.

33
00:02:43,930 --> 00:02:47,150
Yeah, it doesn't really matter, but let's just two, three and four.

34
00:02:48,050 --> 00:02:52,670
And the function that was built into Nampai was NPR Dot.

35
00:02:53,900 --> 00:03:00,740
And then the two arguments our vector one and factor two and the Dot product is nine.

36
00:03:01,760 --> 00:03:04,250
So now we want to define our own function.

37
00:03:04,700 --> 00:03:06,410
And we want to get, of course, the same result.

38
00:03:07,130 --> 00:03:13,250
So what I do is I write def and then I give it a proper name and I just write dot product.

39
00:03:14,660 --> 00:03:16,670
So now we need two arguments.

40
00:03:16,670 --> 00:03:21,770
So I could have caused right vector one Vector two, but it doesn't really matter because these are

41
00:03:21,830 --> 00:03:23,720
like some dummy variables.

42
00:03:24,200 --> 00:03:26,510
So I can write, for example, A and B.

43
00:03:26,930 --> 00:03:32,870
So now only within this definition, A and B are defined as the two arguments.

44
00:03:33,650 --> 00:03:37,790
And they do not have to be the same variable as you're going to use later on.

45
00:03:38,840 --> 00:03:41,990
So now we have to define some value.

46
00:03:43,700 --> 00:03:48,710
But we are going to calculate I call this C, which was really the value of the DOT product.

47
00:03:50,120 --> 00:03:54,230
And to calculate this, I just make it really, really simple.

48
00:03:54,230 --> 00:04:06,830
I just write the first argument or the first component of a times B and then plus I take the Y coordinates

49
00:04:08,300 --> 00:04:13,790
and the Z coordinate, so I just have to make sure these indices here are correct zero, one and two.

50
00:04:14,990 --> 00:04:17,390
So now this is how you calculate the dot product.

51
00:04:17,839 --> 00:04:25,460
Of course, you can make this a much, much nicer by yeah, using some some function, for example.

52
00:04:25,760 --> 00:04:27,560
Or also you can generalize this.

53
00:04:27,560 --> 00:04:33,260
For example, in our case, I have assumed that the A and B vectors have three components.

54
00:04:33,560 --> 00:04:38,480
But of course, you can also define a dot product for vectors which only have two components or even

55
00:04:38,480 --> 00:04:39,320
more components.

56
00:04:39,740 --> 00:04:43,700
But here we want to keep it very simple and just stick to the three component vectors.

57
00:04:44,810 --> 00:04:51,560
So now if we would run this, it would not work yet because the only thing it does is it would calculate

58
00:04:51,560 --> 00:04:54,980
the value of C and it wouldn't do anything with it.

59
00:04:55,640 --> 00:04:59,060
So just for testing purposes, we can write print C.

60
00:05:00,380 --> 00:05:08,420
And now if I make a new cell and I call our new function dot product and now I, of course write our

61
00:05:08,420 --> 00:05:11,910
two vectors, then it will give me the answer.

62
00:05:11,930 --> 00:05:12,410
Nine.

63
00:05:13,520 --> 00:05:15,650
However, here's a problem that we have.

64
00:05:16,790 --> 00:05:20,960
So if we only want to output the number, then this would be OK.

65
00:05:21,590 --> 00:05:27,470
But what if we want to define some variable and assign this value to it as variable?

66
00:05:28,100 --> 00:05:32,090
So then I would write a D as equal to dot product vector one Vector two.

67
00:05:32,360 --> 00:05:35,060
So far, it seems as if it would have worked.

68
00:05:36,710 --> 00:05:41,570
So let's call again the value of D, and you see nothing happens.

69
00:05:42,620 --> 00:05:47,300
So the problem is we have to find that we will print the value of C.

70
00:05:47,900 --> 00:05:51,460
So what you see here is not the value that is assigned to DH.

71
00:05:51,470 --> 00:05:54,590
It's just this print command so that this is a bit tricky.

72
00:05:55,460 --> 00:06:01,760
So if you want to really work with the result, which of course we want, then using print is not a

73
00:06:01,760 --> 00:06:02,240
good idea.

74
00:06:02,960 --> 00:06:07,730
So I use a comment here so that this line doesn't do anything anymore.

75
00:06:07,730 --> 00:06:11,600
And I write here, it's not really useful.

76
00:06:12,500 --> 00:06:16,780
And instead, what do you have to do is you have to write a return return.

77
00:06:16,840 --> 00:06:17,150
See?

78
00:06:18,080 --> 00:06:19,460
So now let's redefine it.

79
00:06:19,490 --> 00:06:20,930
Let's calculate this.

80
00:06:21,470 --> 00:06:26,600
So do you see this time we do not get an output because we did not specify to print anything.

81
00:06:27,680 --> 00:06:35,390
But internally, the value of the product will now be the value of C because we wrote the return C.

82
00:06:35,960 --> 00:06:40,730
And so now we have a scientist value to D and we can call the and it works.

83
00:06:41,030 --> 00:06:44,420
And now, of course, we can also continue to calculate something here.

84
00:06:45,920 --> 00:06:46,430
All right.

85
00:06:46,550 --> 00:06:52,970
So you have seen how to create a function without any arguments and you have seen how to create functions

86
00:06:52,970 --> 00:06:53,930
with arguments.

87
00:06:54,740 --> 00:07:02,660
Now the last thing that I want to show you is how you can use the so-called argument keyword and for

88
00:07:02,660 --> 00:07:02,960
this?

89
00:07:03,320 --> 00:07:04,400
Let me show you an example.

90
00:07:05,840 --> 00:07:11,720
So once again, I define a function and I call this function personal data.

91
00:07:12,980 --> 00:07:16,590
And for the arguments, I use some.

92
00:07:16,650 --> 00:07:19,350
No, so this will become clear in a second.

93
00:07:19,920 --> 00:07:26,550
Name and age, so it should be something like a database of some of these properties of a person.

94
00:07:28,170 --> 00:07:32,790
So, of course, would be a good idea to use a dictionary, but here instead, I want to do something

95
00:07:32,790 --> 00:07:33,120
else.

96
00:07:33,300 --> 00:07:37,200
You will see in a second what I'm going to do, so I will write here.

97
00:07:37,200 --> 00:07:39,960
What the function will do is it will print several things.

98
00:07:40,250 --> 00:07:42,900
So the first thing it will print will be the idea.

99
00:07:43,320 --> 00:07:49,590
So the the number basically like some number in a database or on your passport, you have some number.

100
00:07:50,490 --> 00:07:56,070
So it will print this idea and we merge these two a string.

101
00:07:56,070 --> 00:08:01,650
So we will merge ID and then the space here and then we will turn the number into history.

102
00:08:02,940 --> 00:08:08,310
Then it will print another line, which will be the name.

103
00:08:10,200 --> 00:08:13,020
So once again, we marched a string with another string.

104
00:08:13,350 --> 00:08:17,580
And this time name is typically a strings, or we don't have to convert to type here.

105
00:08:18,090 --> 00:08:20,790
And then the last thing will be to print the H.

106
00:08:21,750 --> 00:08:31,890
So we write Print H merge to string with string h because no, an h o no and h are a numbers.

107
00:08:32,190 --> 00:08:36,419
So we have to convert them to strings first to be able to merge them with the other string.

108
00:08:38,010 --> 00:08:41,850
So now we have to find this function and now let me call the function.

109
00:08:42,570 --> 00:08:48,480
So I write personal data and now I have to provide the arguments.

110
00:08:49,200 --> 00:08:51,270
So let's not 449.

111
00:08:51,270 --> 00:08:52,740
This would be a bit unrealistic.

112
00:08:53,070 --> 00:08:56,460
So let's honor this was the number.

113
00:08:56,460 --> 00:08:57,330
So it would have been fine.

114
00:08:57,330 --> 00:08:58,140
So let's just do it.

115
00:08:59,040 --> 00:09:00,900
Number is 449.

116
00:09:01,350 --> 00:09:06,920
Then the name is, I don't know, Peter and H is twenty nine.

117
00:09:07,560 --> 00:09:09,720
So if we run this, this will be the result.

118
00:09:11,650 --> 00:09:18,910
And of course, if I would have made a mistake here and would have brought here 29 and here 149 down

119
00:09:19,480 --> 00:09:21,010
the ages 449.

120
00:09:22,450 --> 00:09:22,900
All right.

121
00:09:23,650 --> 00:09:27,460
So let's get back to what I wanted to do initially, which is this one.

122
00:09:28,270 --> 00:09:33,010
So now imagine we have here many, many arguments and many, many lines of codes.

123
00:09:33,460 --> 00:09:39,640
And so it can be a bit tricky to remember all of the the order of these arguments and you have to check

124
00:09:39,640 --> 00:09:40,240
every time.

125
00:09:40,930 --> 00:09:43,960
So what we can do instead is we can use the keyboard.

126
00:09:45,110 --> 00:09:47,170
Yeah, the argument keywords.

127
00:09:48,010 --> 00:09:50,660
And so what we're going to do is we're going to write you.

128
00:09:50,680 --> 00:09:54,830
Number is equal to 49.

129
00:09:55,360 --> 00:10:00,330
Name as equal to Peter and H is equal to 29.

130
00:10:00,800 --> 00:10:02,410
Will give exactly the same result.

131
00:10:03,310 --> 00:10:09,250
However, the nice thing about this is that these arguments do not have to be provided in the correct

132
00:10:09,250 --> 00:10:16,000
order anymore so we can do something like this and it still works and it gives us the correct results.

133
00:10:17,530 --> 00:10:22,960
So I think and most of the cases, we will not really use the argument keywords, but it's still a good

134
00:10:22,960 --> 00:10:28,240
to keep this in mind that something like this exists because it can make our lives sometimes a bit more

135
00:10:28,240 --> 00:10:28,660
easy.

