1
00:00:02,310 --> 00:00:08,220
All right, let's finish up the concept of function by talking about what is called the return keyword.

2
00:00:08,550 --> 00:00:08,850
All right.

3
00:00:08,850 --> 00:00:12,220
So before we talk about the return keyword, let's solve this exercise.

4
00:00:12,240 --> 00:00:14,380
So let me comment out this one here.

5
00:00:14,400 --> 00:00:16,079
I don't see many console.log.

6
00:00:16,110 --> 00:00:18,010
Or I can I can comment on this one.

7
00:00:18,030 --> 00:00:18,560
Okay.

8
00:00:18,570 --> 00:00:21,140
So the function goes like this one we can use.

9
00:00:21,150 --> 00:00:22,290
Let me change to use.

10
00:00:22,290 --> 00:00:23,460
I mean, function declaration.

11
00:00:23,460 --> 00:00:26,580
So function function.

12
00:00:26,580 --> 00:00:31,140
And then I say multiply, multiply numbers.

13
00:00:31,170 --> 00:00:37,170
Now my square bracket and my function body here and here, it takes three things, right?

14
00:00:37,170 --> 00:00:41,790
I can say A, B and C in a name here.

15
00:00:41,790 --> 00:00:43,020
Anybody here can see number one.

16
00:00:43,020 --> 00:00:44,010
Number two and I'm three.

17
00:00:44,010 --> 00:00:44,760
It's up to you.

18
00:00:44,880 --> 00:00:48,720
So I will declare variable counts or let end of this one go.

19
00:00:48,960 --> 00:00:56,460
So a result is equal to A, times B, times C.

20
00:00:56,610 --> 00:01:03,150
Now when I call the function multiply num numbers are pass in three things, right?

21
00:01:03,150 --> 00:01:07,380
Pass in them passing one, two and three meaning.

22
00:01:08,310 --> 00:01:10,110
And let me console.log the results here.

23
00:01:10,140 --> 00:01:10,920
I didn't do it.

24
00:01:10,950 --> 00:01:12,510
I'm going to look at the results here.

25
00:01:14,150 --> 00:01:14,610
Okay.

26
00:01:14,630 --> 00:01:21,200
So here A is one, B is two and C is three.

27
00:01:21,320 --> 00:01:26,300
If I console.log it to be one times two, some three, what would be the answer?

28
00:01:26,450 --> 00:01:31,880
One times two is what is two times three six?

29
00:01:32,090 --> 00:01:32,900
Let's try.

30
00:01:33,800 --> 00:01:34,880
Let's save it.

31
00:01:35,510 --> 00:01:37,330
Indeed, we have six.

32
00:01:37,340 --> 00:01:39,170
That is the solution to this question.

33
00:01:39,940 --> 00:01:40,540
Okay.

34
00:01:40,570 --> 00:01:45,460
So last part you talk about is what you call the return keyword.

35
00:01:45,910 --> 00:01:50,200
So let's use this one or let's use this one to explain more.

36
00:01:50,230 --> 00:01:52,030
Okay, let me copy this one here.

37
00:01:52,030 --> 00:01:59,590
Let me comment out and let me comment out this one also and let me paste this function here as before.

38
00:01:59,800 --> 00:02:00,100
Okay.

39
00:02:00,100 --> 00:02:04,030
So let me come in here and call this one the return.

40
00:02:05,170 --> 00:02:11,290
Keyword return after this one, then you'll be a guru when it comes to function creating.

41
00:02:12,000 --> 00:02:12,550
Okay.

42
00:02:12,970 --> 00:02:15,970
So what is the purpose of the return key?

43
00:02:16,150 --> 00:02:17,290
We need a function.

44
00:02:17,470 --> 00:02:19,720
So at this time, right now.

45
00:02:20,110 --> 00:02:27,000
Well, the purpose or the only way this function is doing is that it's taking in some dynamic value.

46
00:02:27,010 --> 00:02:28,470
Make it more easy to read.

47
00:02:28,480 --> 00:02:29,260
Let me see.

48
00:02:29,860 --> 00:02:33,330
Three +47.

49
00:02:33,610 --> 00:02:34,090
We can all.

50
00:02:34,090 --> 00:02:34,540
Really.

51
00:02:35,050 --> 00:02:35,530
Okay.

52
00:02:38,470 --> 00:02:39,280
Let me save it.

53
00:02:40,280 --> 00:02:40,760
Yeah.

54
00:02:41,210 --> 00:02:49,880
So what this function is doing is that, well, it has sort of problems about I mean, we have to centralize

55
00:02:49,880 --> 00:02:57,140
our code in one place, but the governance here is that this function in time, I call this function,

56
00:02:57,380 --> 00:03:00,830
I can call it many times, as many times as I want.

57
00:03:00,860 --> 00:03:01,640
Let's see.

58
00:03:02,030 --> 00:03:04,240
We see we have seven, seven here.

59
00:03:04,250 --> 00:03:05,750
But here comes the problem.

60
00:03:05,960 --> 00:03:12,380
Let's say that I want to display the result of my function into the page here.

61
00:03:13,780 --> 00:03:14,860
But don't worry.

62
00:03:14,860 --> 00:03:18,910
We haven't talked about Dom manipulation, so spare me with that.

63
00:03:19,870 --> 00:03:24,850
I'm going to give you some some scenarios why this function is not performing the way we want it.

64
00:03:25,570 --> 00:03:27,010
That's the scenario, number one.

65
00:03:27,250 --> 00:03:29,410
And also, what about say that?

66
00:03:29,410 --> 00:03:35,220
Oh, no, I don't want this play my result into the page, but instead I want to display it inside an

67
00:03:35,230 --> 00:03:35,950
elect.

68
00:03:36,160 --> 00:03:38,140
How can we do that?

69
00:03:38,650 --> 00:03:47,320
In that way, we need what is called the return keyword, because this function is not giving out anything.

70
00:03:47,740 --> 00:03:48,970
It just console.log in it.

71
00:03:49,150 --> 00:03:50,110
So there were ten.

72
00:03:50,110 --> 00:03:57,940
Key is used to exit a function and return a value to the caller.

73
00:03:58,600 --> 00:03:59,590
Here comes.

74
00:03:59,950 --> 00:04:04,750
So one point of return is it is used to exit a function.

75
00:04:05,200 --> 00:04:07,390
I would I would explain to you with code right now.

76
00:04:07,830 --> 00:04:08,200
Okay.

77
00:04:08,560 --> 00:04:17,170
And then if a value is present in the return, it is going to give out to the variable we assign a function

78
00:04:17,170 --> 00:04:17,560
to.

79
00:04:18,370 --> 00:04:22,870
The return keyword is essential for creating functions that return value.

80
00:04:22,880 --> 00:04:24,820
So this function is returning values.

81
00:04:24,910 --> 00:04:31,480
So without it, function will only be able to perform actions but not return values to the caller.

82
00:04:31,600 --> 00:04:37,240
This would limit the usefulness of a functions and make them much less powerful.

83
00:04:37,990 --> 00:04:40,690
So that's what we talk about right now, right?

84
00:04:40,870 --> 00:04:47,900
Because this function, even though it's doing some action, but it's not doing it's not more useful.

85
00:04:47,920 --> 00:04:50,260
That's why we tend to come in here.

86
00:04:50,680 --> 00:04:52,840
So here it comes.

87
00:04:52,870 --> 00:04:57,030
Now, instead of returning a console of this one here.

88
00:04:57,130 --> 00:05:01,960
Now you bring in the return keyword from JavaScript.

89
00:05:02,880 --> 00:05:06,630
And then what are we giving out or return from?

90
00:05:06,630 --> 00:05:08,550
This function is the result.

91
00:05:09,570 --> 00:05:13,650
So many that JavaScript runs our code line by line.

92
00:05:13,680 --> 00:05:16,170
If it gets here, first of all, declare a variable.

93
00:05:16,170 --> 00:05:22,530
And here you go to a line number eight where I have a variable called result and give me X plus Y,

94
00:05:22,530 --> 00:05:23,880
which is three plus seven.

95
00:05:23,880 --> 00:05:28,110
Let me remove this one from here and let me save it.

96
00:05:28,200 --> 00:05:33,180
Now, see that we have nothing here, even though we have the risk, but because we have removed the

97
00:05:33,180 --> 00:05:39,750
console.log and then we are using the return keyword, how are we going to get the result here?

98
00:05:40,110 --> 00:05:40,920
Here comes.

99
00:05:41,160 --> 00:05:45,330
So here we have given the result to return.

100
00:05:45,540 --> 00:05:54,630
It means that when you call it a function, we have the value inside the return keyword with the function.

101
00:05:55,290 --> 00:05:59,610
So to anytime, use a function, use a return keyword.

102
00:05:59,730 --> 00:06:04,560
Again, when you call it, you assign the result to a variable.

103
00:06:04,590 --> 00:06:06,060
You can see that led.

104
00:06:06,300 --> 00:06:13,950
Now we can say and so is equal to now because of the return keyword.

105
00:06:14,460 --> 00:06:19,200
This one represents the result coming out from this function.

106
00:06:19,380 --> 00:06:23,250
So now behind the scene, this is seven, right?

107
00:06:23,250 --> 00:06:30,780
And it's now in the variable of and so I can take this variable and use it wherever I want.

108
00:06:31,020 --> 00:06:32,520
I can console.log it.

109
00:06:32,880 --> 00:06:36,750
I can display it here and wherever I want display I can use it.

110
00:06:36,750 --> 00:06:41,040
So if I console.log the answer.

111
00:06:42,090 --> 00:06:42,810
Check it out.

112
00:06:43,020 --> 00:06:45,240
Now, you see, I have seven.

113
00:06:46,380 --> 00:06:47,250
I have seven.

114
00:06:47,250 --> 00:06:48,540
That is 40.

115
00:06:48,540 --> 00:06:54,420
Or if I don't want to call it here, I can use a method called select.

116
00:06:54,510 --> 00:06:56,820
I let that display something in the browser.

117
00:06:57,390 --> 00:07:00,360
Okay, so here I select and then my answer.

118
00:07:01,280 --> 00:07:02,450
If I save it.

119
00:07:02,480 --> 00:07:06,950
Now, you see that we have taken the result and display it here.

120
00:07:07,970 --> 00:07:15,560
Now our function is more reusable because now we can take this function and then display the result

121
00:07:15,560 --> 00:07:17,000
any way I want.

122
00:07:17,960 --> 00:07:23,930
Let's talk about the exit, anywhere or inside, any function.

123
00:07:24,290 --> 00:07:32,720
If it sees return keyword it exit meaning is top the the function or the logic inside of a function.

124
00:07:32,930 --> 00:07:44,480
So now to prove to you let's console.log something here console dot log let's say function code like

125
00:07:44,480 --> 00:07:45,080
this one.

126
00:07:45,620 --> 00:07:47,720
So if I save it, have a look.

127
00:07:47,720 --> 00:07:48,830
Let me remove this console.

128
00:07:48,830 --> 00:07:49,130
Let me.

129
00:07:49,130 --> 00:07:49,760
Alexia.

130
00:07:51,330 --> 00:07:57,600
If I press the piece, I see what function call, because when I call this function, it is reading

131
00:07:57,600 --> 00:07:58,530
line by line.

132
00:07:58,620 --> 00:08:01,560
It gets here x plus x, which is five.

133
00:08:01,590 --> 00:08:02,300
Give it to this one.

134
00:08:02,310 --> 00:08:03,330
Go to console.log.

135
00:08:03,360 --> 00:08:07,890
That's why it has to be displayed here, because we have called a function here.

136
00:08:08,400 --> 00:08:12,120
So but let me say function.

137
00:08:12,540 --> 00:08:17,340
Let me say before before return keyword.

138
00:08:18,060 --> 00:08:19,080
Before retain.

139
00:08:19,080 --> 00:08:20,670
And then the same thing here.

140
00:08:21,830 --> 00:08:25,130
We're down here and say after ten.

141
00:08:25,280 --> 00:08:28,280
So when I save this one, we see that.

142
00:08:28,310 --> 00:08:29,660
Let me save this function.

143
00:08:30,350 --> 00:08:38,330
You can see that this one has called the first one, meaning the function before return has caught.

144
00:08:38,330 --> 00:08:39,200
But this one.

145
00:08:39,620 --> 00:08:44,730
You see that this one has been degraded, meaning this punch will never be called.

146
00:08:44,750 --> 00:08:47,960
As soon as it gets to this return, it exits.

147
00:08:47,990 --> 00:08:49,340
The function will not continue.

148
00:08:49,370 --> 00:08:51,250
That's why we couldn't call this one.

149
00:08:51,260 --> 00:08:57,590
But if I remove the return from here and save it, we see the two of them has been called.

150
00:08:57,650 --> 00:09:01,310
That's the purpose of a function, the return keyword.

