0
1
00:00:00,000 --> 00:00:03,901
Okay so we're going to see how
to use functions with type
1

2
00:00:03,901 --> 00:00:09,981
script a very important topic.
So it's exactly the same as in
2

3
00:00:09,981 --> 00:00:12,941
obviously so we're going to
create a function for example
3

4
00:00:12,941 --> 00:00:18,621
sum that's going to receive a
parameter A and parameter B and
4

5
00:00:18,621 --> 00:00:24,461
we're just going to return A
plus B so by default if you do
5

6
00:00:24,461 --> 00:00:30,921
that let's say you get the
result here of sum and 3
6

7
00:00:30,921 --> 00:00:35,721
+ 4.  It's going to work,
but as you can see the result
7

8
00:00:35,721 --> 00:00:39,721
is of type any so you don't
know what this is it's not
8

9
00:00:39,721 --> 00:00:43,401
typed correctly and this is
because by default the
9

10
00:00:43,401 --> 00:00:47,961
parameters that you're sending
here have no types , they have
10

11
00:00:47,961 --> 00:00:54,201
the any types so this is
definitely not good because I
11

12
00:00:54,201 --> 00:00:58,681
can send something like this
okay and if I do something like
12

13
00:00:58,681 --> 00:01:02,641
this I try to console log
results well the behavior is
13

14
00:01:02,641 --> 00:01:06,161
not going to be exactly what I
expect as you can see it's 34
14

15
00:01:06,161 --> 00:01:10,721
and this is because 3 4 have
been concatinated since A is a
15

16
00:01:10,721 --> 00:01:14,321
string that will just be joined
okay instead of being really
16

17
00:01:14,321 --> 00:01:18,161
calculated so you really want
to make sure that you're really
17

18
00:01:18,161 --> 00:01:22,401
sending a number to have the
expected behaviour so to do
18

19
00:01:22,401 --> 00:01:25,041
that it's pretty simple you can
type your parameters doing
19

20
00:01:25,041 --> 00:01:32,241
something like this number a
number now it's not possible
20

21
00:01:32,241 --> 00:01:35,841
anymore to do something like
this okay it's going to say no
21

22
00:01:35,841 --> 00:01:38,961
you're sending a string it's
not assignable to a parameter
22

23
00:01:38,961 --> 00:01:45,721
of type number this one So
cool. Now this is protected.
23

24
00:01:45,721 --> 00:01:48,441
And now as you can see
automatically the result is now
24

25
00:01:48,441 --> 00:01:51,961
of type number absolutely
because here I'm returning a
25

26
00:01:51,961 --> 00:01:55,081
number plus a number which
gives a number okay so by
26

27
00:01:55,081 --> 00:01:59,081
default if I pass my mouse over
my function as you can see it
27

28
00:01:59,081 --> 00:02:02,281
says that this is the function
and at the end you can see it's
28

29
00:02:02,281 --> 00:02:05,881
returning something of type
number so it has been correctly
29

30
00:02:05,881 --> 00:02:11,161
typed by inference now I'm
going to show you how to send
30

31
00:02:11,161 --> 00:02:16,281
parameters that are optionals
and how to send  default values
31

32
00:02:16,281 --> 00:02:20,921
to a function. So now I'm going
to remove that. I'm going to
32

33
00:02:20,921 --> 00:02:26,601
comment all of this. Now let's
say I create a function that's
33

34
00:02:26,601 --> 00:02:33,121
going to be apply discount.
That is going to receive a
34

35
00:02:33,121 --> 00:02:36,481
price that is going to be a
number and a percentage of
35

36
00:02:36,481 --> 00:02:40,961
discount that should be applied
on this price so it will be
36

37
00:02:40,961 --> 00:02:48,001
discount percentage it's also
going to be a number and I'm
37

38
00:02:48,001 --> 00:02:54,081
just going to return the price
minus the price multiplied by
38

39
00:02:54,081 --> 00:03:02,341
discount percentage divided by
100 okay now I can use it for
39

40
00:03:02,341 --> 00:03:06,761
example like this final price
and I'm going to apply a
40

41
00:03:06,761 --> 00:03:09,241
discount on a price so for
example the price is going to
41

42
00:03:09,241 --> 00:03:16,221
be 300 and I'm going to I don't
know apply 50% discount okay
42

43
00:03:16,221 --> 00:03:20,781
and I'm going to log that final
price
43

44
00:03:23,221 --> 00:03:27,561
so as you can see yes it's
working I have 50% applied on
44

45
00:03:27,561 --> 00:03:32,921
300 cool so if I try to send
something like this it's not
45

46
00:03:32,921 --> 00:03:37,721
going to work but now let's say
I work in a company I mean a
46

47
00:03:37,721 --> 00:03:45,081
team and someone as to make some
changes in my function, and
47

48
00:03:45,081 --> 00:03:49,081
for some reason the developer
here start working on this
48

49
00:03:49,081 --> 00:03:51,961
apply function and do some
modification and return
49

50
00:03:51,961 --> 00:03:56,221
something else okay for example
this doesn't really make sense
50

51
00:03:56,221 --> 00:03:59,901
but you will get the idea. So
what I could have done is
51

52
00:03:59,901 --> 00:04:02,381
something like this. I could
have typed my functions
52

53
00:04:02,381 --> 00:04:05,581
returned. Okay, I could have
said alright this function is
53

54
00:04:05,581 --> 00:04:08,461
always going to return a
number. Now anyone that is
54

55
00:04:08,461 --> 00:04:11,021
going to work with this
function will know that oh okay
55

56
00:04:11,021 --> 00:04:13,901
it's going to receive a price
that is a number, a discount
56

57
00:04:13,901 --> 00:04:16,461
percentage that is a number and
this function is going to
57

58
00:04:16,461 --> 00:04:20,061
return a number. So there is no
way I could do something like
58

59
00:04:20,061 --> 00:04:26,141
this, okay? So this is going to
help the next developer as well
59

60
00:04:26,141 --> 00:04:29,901
and also yourself because if
you do something wrong at some
60

61
00:04:29,901 --> 00:04:35,201
point and add a string
somewhere like this where you
61

62
00:04:35,201 --> 00:04:38,401
will have an error because this
is going to now be a string and
62

63
00:04:38,401 --> 00:04:42,001
it's not going to work okay all
right cool so now let's talk
63

64
00:04:42,001 --> 00:04:45,441
about the optional parameters
so in a function you can
64

65
00:04:45,441 --> 00:04:48,081
actually send optional
parameters let me give you an
65

66
00:04:48,081 --> 00:04:54,561
example let's say you want to
add a parameter that is give to
66

67
00:04:54,561 --> 00:04:59,521
charity and it's a boolean if
and if it's true then you're
67

68
00:04:59,521 --> 00:05:04,461
going to give one euro of the
price that you have sent here.
68

69
00:05:04,461 --> 00:05:07,901
So I'm just going to make a
small change here. It will be a
69

70
00:05:07,901 --> 00:05:12,141
new price, I'm going to store
that.
70

71
00:05:15,221 --> 00:05:20,681
And if "Give to charity" then I'm just
going to remove one of my
71

72
00:05:20,681 --> 00:05:25,941
price and in the end I'm just
going to return your price like
72

73
00:05:25,941 --> 00:05:30,261
this now as you can see I'm
missing one parameter okay
73

74
00:05:30,261 --> 00:05:34,821
expected argument "3" but got
2 only so you have to add
74

75
00:05:34,821 --> 00:05:40,341
give to charity okay so I could
write true and as you can see
75

76
00:05:40,341 --> 00:05:44,581
yes it's working but you could
actually make this parameter
76

77
00:05:44,581 --> 00:05:49,621
optional you could add here a
question mark and by doing that
77

78
00:05:49,621 --> 00:05:54,261
now it's not required anymore
to send this parameter okay
78

79
00:05:54,261 --> 00:05:58,841
it's going to work as well and
since I'm controlling here, the
79

80
00:05:58,841 --> 00:06:02,201
parameter, everything is going
to work fine. So you can make
80

81
00:06:02,201 --> 00:06:05,001
your parameters optionally
here. But be careful, you can
81

82
00:06:05,001 --> 00:06:10,041
never have your optional
parameters before required
82

83
00:06:10,041 --> 00:06:13,401
parameters, okay? You cannot do
something like this. This is
83

84
00:06:13,401 --> 00:06:15,881
not going to work and you will
actually have a message. A
84

85
00:06:15,881 --> 00:06:19,081
required parameter cannot
follow an optional parameters.
85

86
00:06:19,081 --> 00:06:22,441
All the optionals parameter
should be at the end of your
86

87
00:06:22,441 --> 00:06:28,281
parameters. Okay? Now you can
also set a default value for
87

88
00:06:28,281 --> 00:06:32,681
your parameters exactly like
you would in JavaScript
88

89
00:06:32,681 --> 00:06:36,681
actually so for example I could
do something like this for my
89

90
00:06:36,681 --> 00:06:39,721
discount percentage I could say
that by default is going to be
90

91
00:06:39,721 --> 00:06:44,841
50 and now it's actually also
going to become an optional
91

92
00:06:44,841 --> 00:06:48,041
parameter behind the scene but
it will have a default value if
92

93
00:06:48,041 --> 00:06:52,601
not specified so if I do this
you will see that it will still
93

94
00:06:52,601 --> 00:06:56,761
be fifty okay as you can see
it's still 50 because by
94

95
00:06:56,761 --> 00:07:00,281
default is going to add this
value alright So that is pretty
95

96
00:07:00,281 --> 00:07:02,921
much it about the functions,
you just have to remember that
96

97
00:07:02,921 --> 00:07:05,641
you have to type all the
parameters that you can make
97

98
00:07:05,641 --> 00:07:10,201
then optional or not, you can
send a default value and if you
98

99
00:07:10,201 --> 00:07:12,121
send a default value, it's
going to be an optional
99

100
00:07:12,121 --> 00:07:15,801
parameter, you can also type
the whole functions, so what is
100

101
00:07:15,801 --> 00:07:20,121
going to return, and it's a
more strict approach because it
101

102
00:07:20,121 --> 00:07:24,281
will force the next developer to
match the return type, and yes, that
102

103
00:07:24,281 --> 00:07:27,961
is pretty much it about the
functions. Okay, so, let's
103

104
00:07:27,961 --> 00:07:32,501
continue in the next video and
soon we'll do an exercise.
