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