1
00:00:02,050 --> 00:00:02,883
So now we've got

2
00:00:02,883 --> 00:00:04,790
some key basics about functions,

3
00:00:04,790 --> 00:00:06,050
but we're still not done

4
00:00:06,050 --> 00:00:09,310
because this function is still not ideal,

5
00:00:09,310 --> 00:00:10,960
it's still not perfect.

6
00:00:10,960 --> 00:00:14,850
It works but we have a couple of bad practices in there,

7
00:00:14,850 --> 00:00:17,340
which we don't typically wanna do.

8
00:00:17,340 --> 00:00:20,510
I did explain why we can change adult years

9
00:00:20,510 --> 00:00:22,193
if our code looks like this.

10
00:00:23,060 --> 00:00:26,660
But maybe you get the feeling over the last lecture that

11
00:00:26,660 --> 00:00:28,490
that's a lot to memorize,

12
00:00:28,490 --> 00:00:29,610
and it is.

13
00:00:29,610 --> 00:00:32,470
And sometimes you need this behavior,

14
00:00:32,470 --> 00:00:33,550
which I described,

15
00:00:33,550 --> 00:00:35,810
that you can shadow variables

16
00:00:35,810 --> 00:00:38,700
and that you don't wanna do it in other scenarios

17
00:00:38,700 --> 00:00:41,083
but you can also make life easier for you.

18
00:00:41,990 --> 00:00:46,220
It would be best if this function doesn't even have to know

19
00:00:46,220 --> 00:00:50,830
about the variable name of our outside variable here,

20
00:00:50,830 --> 00:00:53,140
if it could work stand alone.

21
00:00:53,140 --> 00:00:54,210
Because at the moment,

22
00:00:54,210 --> 00:00:57,890
if I ever changed this from adult years to,

23
00:00:57,890 --> 00:01:00,830
let's say total adult years,

24
00:01:00,830 --> 00:01:04,470
I have to change this name down here

25
00:01:04,470 --> 00:01:06,230
and down here,

26
00:01:06,230 --> 00:01:08,123
and also inside of the function.

27
00:01:09,240 --> 00:01:10,073
And again,

28
00:01:10,073 --> 00:01:11,370
because this is a simple script

29
00:01:11,370 --> 00:01:12,930
that's not too difficult.

30
00:01:12,930 --> 00:01:14,930
But you always have to keep in mind

31
00:01:14,930 --> 00:01:18,170
that ultimately you will write more complex code

32
00:01:18,170 --> 00:01:20,290
and then having to make changes,

33
00:01:20,290 --> 00:01:21,850
in dozens of places,

34
00:01:21,850 --> 00:01:25,460
maybe even across multiple JavaScript files,

35
00:01:25,460 --> 00:01:27,083
is very annoying.

36
00:01:28,570 --> 00:01:29,403
So therefore,

37
00:01:29,403 --> 00:01:31,430
it would be better if this function

38
00:01:31,430 --> 00:01:35,500
wouldn't even have to know this external variable name.

39
00:01:35,500 --> 00:01:38,910
And indeed that is something you typically wanna strive for,

40
00:01:38,910 --> 00:01:40,500
that your functions work

41
00:01:40,500 --> 00:01:43,973
without knowing external variable names.

42
00:01:44,890 --> 00:01:48,350
And when it comes to deriving this result here,

43
00:01:48,350 --> 00:01:50,490
this result of this calculation,

44
00:01:50,490 --> 00:01:52,190
what we can do in a function

45
00:01:52,190 --> 00:01:56,490
is we can return the result of a calculation

46
00:01:56,490 --> 00:01:59,383
with the special return keyword.

47
00:02:01,200 --> 00:02:04,040
Now, what does returning a value mean?

48
00:02:04,040 --> 00:02:06,980
It means that this function does now

49
00:02:06,980 --> 00:02:10,090
not just perform a certain operation

50
00:02:10,090 --> 00:02:13,330
or possibly multiple steps.

51
00:02:13,330 --> 00:02:15,830
You can have multiple lines of code in there,

52
00:02:15,830 --> 00:02:17,230
that would be fine.

53
00:02:17,230 --> 00:02:20,860
But that instead when you call that function,

54
00:02:20,860 --> 00:02:24,910
you now get a value from calling that function.

55
00:02:24,910 --> 00:02:27,070
So before this function,

56
00:02:27,070 --> 00:02:28,830
without the return keyword,

57
00:02:28,830 --> 00:02:30,920
didn't return anything.

58
00:02:30,920 --> 00:02:34,523
Calling that function doesn't produce a new value.

59
00:02:35,530 --> 00:02:37,500
Now with the return statement,

60
00:02:37,500 --> 00:02:40,830
calling that function does produce a new value

61
00:02:40,830 --> 00:02:43,910
and calling that function does return this result.

62
00:02:43,910 --> 00:02:46,390
Hence, we can work with that result here

63
00:02:46,390 --> 00:02:49,070
in the line of code where we called this function.

64
00:02:49,070 --> 00:02:50,340
And we can, for example,

65
00:02:50,340 --> 00:02:52,173
store it in a variable again.

66
00:02:53,120 --> 00:02:57,270
In total adult years, for example.

67
00:02:57,270 --> 00:03:01,003
Here we can store the result of calling that function.

68
00:03:02,070 --> 00:03:02,903
Now, of course,

69
00:03:02,903 --> 00:03:03,940
that still means we're using

70
00:03:03,940 --> 00:03:05,640
that variable name down there.

71
00:03:05,640 --> 00:03:07,100
And if we were to change it,

72
00:03:07,100 --> 00:03:09,870
we have to change it in these places down there.

73
00:03:09,870 --> 00:03:11,830
But it's very often the case

74
00:03:11,830 --> 00:03:16,080
that you store functions like this in separate files.

75
00:03:16,080 --> 00:03:17,210
And then you at least

76
00:03:17,210 --> 00:03:20,110
don't have to dive into those files as well,

77
00:03:20,110 --> 00:03:23,070
to make such variable name changes.

78
00:03:23,070 --> 00:03:24,730
And in your main file

79
00:03:24,730 --> 00:03:26,810
where you are using a variable

80
00:03:26,810 --> 00:03:28,720
in different places in your code,

81
00:03:28,720 --> 00:03:30,350
you can always use the search

82
00:03:30,350 --> 00:03:34,760
and replace feature of your IDE of Visual Studio Code,

83
00:03:34,760 --> 00:03:37,110
to quickly change the name in all the places

84
00:03:37,110 --> 00:03:38,810
where it needs to be changed.

85
00:03:38,810 --> 00:03:41,400
So this typically is a better way of doing it

86
00:03:41,400 --> 00:03:43,500
than what we did before.

87
00:03:43,500 --> 00:03:44,700
And in addition

88
00:03:44,700 --> 00:03:48,110
and even more importantly than this,

89
00:03:48,110 --> 00:03:52,030
we don't have to change the name everywhere thing,

90
00:03:52,030 --> 00:03:55,060
we now also made a huge step

91
00:03:55,060 --> 00:03:58,820
towards making our function more reusable,

92
00:03:58,820 --> 00:04:01,080
more flexible.

93
00:04:01,080 --> 00:04:03,030
Before we returned,

94
00:04:03,030 --> 00:04:05,550
we always stored the result

95
00:04:05,550 --> 00:04:09,270
of our calculation in total adult years,

96
00:04:09,270 --> 00:04:11,440
inside of that function.

97
00:04:11,440 --> 00:04:12,670
And that means

98
00:04:12,670 --> 00:04:15,870
that if we ever would want to use

99
00:04:15,870 --> 00:04:19,329
this function in a different place of our code

100
00:04:19,329 --> 00:04:22,700
and store the result in a different variable,

101
00:04:22,700 --> 00:04:24,280
it's just wouldn't work,

102
00:04:24,280 --> 00:04:27,800
as long as we directly referenced total adult years

103
00:04:27,800 --> 00:04:29,773
from inside this function.

104
00:04:30,810 --> 00:04:34,070
Now with the return statement that changed,

105
00:04:34,070 --> 00:04:37,690
now we can use this function anywhere in our code

106
00:04:37,690 --> 00:04:40,310
and in the place where we use it.

107
00:04:40,310 --> 00:04:43,890
So in the place where we call this function,

108
00:04:43,890 --> 00:04:47,810
we can then decide where the result should be stored.

109
00:04:47,810 --> 00:04:51,780
And that is a huge benefit of using the return statement,

110
00:04:51,780 --> 00:04:55,460
instead of directly accessing a variable in the function

111
00:04:55,460 --> 00:04:57,560
and storing our value there,

112
00:04:57,560 --> 00:05:02,550
because now our function is more reusable and more flexible,

113
00:05:02,550 --> 00:05:05,610
even though that's not something we utilize

114
00:05:05,610 --> 00:05:08,113
or need for this simple demo here.

115
00:05:09,040 --> 00:05:11,870
But it is something you typically wanna go for

116
00:05:11,870 --> 00:05:13,720
when you write code.

117
00:05:13,720 --> 00:05:14,940
Because ultimately,

118
00:05:14,940 --> 00:05:18,700
it will make your life as a developer easier.

119
00:05:18,700 --> 00:05:20,400
We also of course,

120
00:05:20,400 --> 00:05:22,300
wanna do that down here,

121
00:05:22,300 --> 00:05:25,223
where I call calculate adult years again.

122
00:05:26,460 --> 00:05:29,840
Because we now need to store that return value,

123
00:05:29,840 --> 00:05:32,670
otherwise that returned value goes into the void

124
00:05:32,670 --> 00:05:35,283
and we won't be able to work with it.

125
00:05:37,340 --> 00:05:40,020
Now, if I save this again,

126
00:05:40,020 --> 00:05:44,500
I again get these alerts with 14 and 27.

127
00:05:44,500 --> 00:05:46,130
Let me close this and reload

128
00:05:46,130 --> 00:05:47,943
to make that a bit easier to read.

129
00:05:48,950 --> 00:05:51,470
And that's a great step forward

130
00:05:51,470 --> 00:05:54,040
and a very important feature,

131
00:05:54,040 --> 00:05:57,370
functions don't have to return the value.

132
00:05:57,370 --> 00:06:00,400
For example, alert here doesn't return a value.

133
00:06:00,400 --> 00:06:03,300
We're not storing this anywhere,

134
00:06:03,300 --> 00:06:05,220
but they often do.

135
00:06:05,220 --> 00:06:07,070
And especially if you have a function

136
00:06:07,070 --> 00:06:10,010
that performs a certain mathematical operation,

137
00:06:10,010 --> 00:06:12,730
it's very common that you do want to return

138
00:06:12,730 --> 00:06:14,920
so that the result of this operation

139
00:06:14,920 --> 00:06:19,363
then can be stored in different variables in your code.

