Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:01,930
So functions are.
2
00:00:02,130 --> 00:00:08,010
If you've ever heard the term functions, chances are you probably had some bad dreams about it.
3
00:00:08,580 --> 00:00:14,710
Usually students find this term hard to grasp at first or second or ever.
4
00:00:14,850 --> 00:00:20,220
They usually do not thoroughly understand this topic, but somehow managed to move on.
5
00:00:20,400 --> 00:00:25,110
And since in programming, we have a lot of things related to one another.
6
00:00:25,230 --> 00:00:30,690
Students who act the understanding of the functions topic tend to fill these gaps in their knowledge,
7
00:00:30,750 --> 00:00:35,610
and they will feel difficulties with most of the topics that will follow afterward.
8
00:00:35,790 --> 00:00:42,420
That's why I recommend and I think it's better to learn it once and learn it well and only then.
9
00:00:42,480 --> 00:00:48,390
Once you've done it, you are allowed to proceed to more advanced sections and sections that you are
10
00:00:48,390 --> 00:00:52,650
going to use functions in your programming language.
11
00:00:52,770 --> 00:00:58,770
So in this section, the goal is to do our best to understand what are these functions?
12
00:00:58,980 --> 00:01:00,450
Where do they come from?
13
00:01:00,690 --> 00:01:02,220
Why do we need them for?
14
00:01:02,520 --> 00:01:05,040
And why shouldn't we be so afraid of them?
15
00:01:05,220 --> 00:01:12,660
I will try to give you an overview of real life situations that resemble in somewhat to how functions
16
00:01:12,660 --> 00:01:14,160
are being used in programming.
17
00:01:14,280 --> 00:01:20,940
And only once you get a hold of why we may be interested in using functions in general only then you
18
00:01:20,940 --> 00:01:26,550
may consider moving on to use functions as part of your code in your programming language.
19
00:01:26,670 --> 00:01:27,750
So here we go.
20
00:01:27,870 --> 00:01:32,930
Grab a cup of your favorite drink, take a comfortable posture and let's go.
21
00:01:33,120 --> 00:01:39,450
So I want you guys to think about a situation where we have Bob trying to develop his own newest and
22
00:01:39,450 --> 00:01:45,510
coolest application that should address students needs and show them important information regarding
23
00:01:45,540 --> 00:01:46,710
their grades.
24
00:01:46,830 --> 00:01:52,320
So we can see here, Bob and Bob is there all alone in this story.
25
00:01:52,320 --> 00:01:55,960
And he's trying trying his best to build this application.
26
00:01:56,160 --> 00:02:02,040
But there are so many aspects that Bob needs to take care of, like getting grades from the users,
27
00:02:02,070 --> 00:02:06,750
calculating every student's grade, printing every user's information to the screen.
28
00:02:07,080 --> 00:02:10,920
And there is a lot of other students and there is a lot of students.
29
00:02:10,950 --> 00:02:13,240
There is a lot of things that Bob has to do.
30
00:02:13,380 --> 00:02:15,450
So why am I saying that?
31
00:02:15,450 --> 00:02:15,690
What?
32
00:02:15,980 --> 00:02:17,670
Why do I even say that?
33
00:02:17,670 --> 00:02:22,010
Bob has a lot of things to do and there is a lot of students and so on and so forth.
34
00:02:22,170 --> 00:02:28,020
Basically, what I want you to do in this example is that I want you to think of Bob as the main function,
35
00:02:28,110 --> 00:02:30,930
the main block we've used in the course all along.
36
00:02:31,050 --> 00:02:39,390
So on one hand, it seems like Bob could take care of all these aspects by on his own and all the code
37
00:02:39,390 --> 00:02:42,630
of getting grades, calculating the average grade and so on.
38
00:02:42,930 --> 00:02:46,320
All of that is going to be under the main block.
39
00:02:46,470 --> 00:02:48,090
Just write down a bunch.
40
00:02:48,150 --> 00:02:53,070
A very large bunch of code in the main section and help that this program will work.
41
00:02:53,490 --> 00:02:55,290
So that's pretty much what we are.
42
00:02:55,380 --> 00:02:58,770
We're used to doing all the exercises so far.
43
00:02:58,800 --> 00:02:59,140
Right.
44
00:02:59,310 --> 00:03:05,910
We simply gave Bob to take care of everything because we said, like in this analogy, that Bob is just
45
00:03:05,910 --> 00:03:07,670
like our main function.
46
00:03:07,700 --> 00:03:07,970
Okay.
47
00:03:08,310 --> 00:03:15,080
But the main question that should be asked here is whether this approach is correct or wrong.
48
00:03:15,270 --> 00:03:20,490
Writing all the code under the main section is probably going to be very tedious.
49
00:03:20,520 --> 00:03:27,300
And not only that, but we will also be able to see that there are many repetitive operations that we
50
00:03:27,300 --> 00:03:27,660
do.
51
00:03:27,750 --> 00:03:33,780
For example, calculating every student's grade or printing every user's information should be pretty
52
00:03:33,780 --> 00:03:35,820
much the same for all users.
53
00:03:35,850 --> 00:03:36,180
Right.
54
00:03:36,310 --> 00:03:44,430
And I mean, it's it's kind of obvious that there is a lot of commands that are going on over and over
55
00:03:44,430 --> 00:03:46,650
again, and they are pretty much the same.
56
00:03:46,860 --> 00:03:51,210
So why should we inflate our program with a lot of repetitive lines of code?
57
00:03:51,600 --> 00:03:53,490
It simply doesn't make any sense.
58
00:03:54,000 --> 00:04:00,540
And also, if sometimes in the future, once you release your program, if you will have some bug and
59
00:04:00,600 --> 00:04:03,330
that may happen, how will you track the bug?
60
00:04:03,540 --> 00:04:04,740
There is only Bob.
61
00:04:04,770 --> 00:04:11,070
And you should go over all of his commands, probably line by line and try to track down where is the
62
00:04:11,070 --> 00:04:11,640
problem.
63
00:04:11,790 --> 00:04:17,310
So that's also another reason why you shouldn't leave all the code in just Bob's hands.
64
00:04:17,610 --> 00:04:21,510
That's why it shouldn't be only in one main section.
65
00:04:21,630 --> 00:04:28,020
So for these reasons we've just mentioned so far and for many reasons, we are going to mention also
66
00:04:28,020 --> 00:04:36,300
later on, for these reasons, we decided that we are going to use functions if we know that the process
67
00:04:36,300 --> 00:04:40,260
of calculating the average grade for every student is pretty much the same.
68
00:04:40,680 --> 00:04:47,250
Then we would like to create some guy, let's call him Mike for this example, that his goal is, for
69
00:04:47,250 --> 00:04:54,720
example, to be in charge of getting some grades from Bob and calculating the average grade between
70
00:04:54,720 --> 00:04:54,930
them.
71
00:04:55,080 --> 00:04:59,670
So all these guy, all this Mike knows to do is simply to receive a.
72
00:04:59,960 --> 00:05:07,040
Full of grades and then calculate the average grade and finally return the result to Bob in this example,
73
00:05:07,130 --> 00:05:13,460
meaning that if we have to calculate the average grade for 10 people, then the calculation process
74
00:05:13,460 --> 00:05:16,130
is going to be the same for all of them.
75
00:05:16,130 --> 00:05:16,460
Right.
76
00:05:16,630 --> 00:05:22,430
Mike will just sum up all the grades he has been given and then divide the sum by the total number of
77
00:05:22,430 --> 00:05:22,940
grades.
78
00:05:23,090 --> 00:05:25,240
That's how you'd calculate an average grade.
79
00:05:25,430 --> 00:05:29,450
So we can say that Mike will be in charge just of this task.
80
00:05:29,540 --> 00:05:34,730
Only he will not read inputs from the user and he will not print anything to the screen.
81
00:05:35,060 --> 00:05:43,970
He's here just to help Bob to accomplish everything he needs to get started, to get calculate, to
82
00:05:43,970 --> 00:05:45,920
get his program on track.
83
00:05:46,040 --> 00:05:52,820
Bob will give Mike the grades of some user user one, for example, and then Mike will only calculate
84
00:05:52,820 --> 00:05:56,030
the average grade and return the results to Bob.
85
00:05:56,210 --> 00:05:58,070
That's all Mike will do.
86
00:05:58,160 --> 00:06:02,270
Bob calls Mike and gives him a couple of grades.
87
00:06:02,600 --> 00:06:06,260
Mike finds out the average and returns to Bob.
88
00:06:06,470 --> 00:06:07,160
That's it.
89
00:06:07,280 --> 00:06:13,910
So once again, if there is some repetitive process of doing almost the same task over and over again,
90
00:06:14,330 --> 00:06:18,990
we would like to use some skilled person for doing these kind of tasks.
91
00:06:19,100 --> 00:06:24,920
So for the overall task that we specified previously, there will be a couple of specialists.
92
00:06:25,340 --> 00:06:32,840
The first one knows how to print a greeting message to the to the screen and on the second one knows
93
00:06:32,840 --> 00:06:34,790
how to calculate the average grade.
94
00:06:35,000 --> 00:06:36,290
That's our mike.
95
00:06:36,650 --> 00:06:43,010
And the third one will know we'll be capable of printing the average grade nudge, calculating the average
96
00:06:43,010 --> 00:06:47,180
grade, but printing the average grade through the screen and so on.
97
00:06:47,330 --> 00:06:53,900
So there may be a lot of people with a lot of specialties that will help Bob to organize his application
98
00:06:53,900 --> 00:06:56,720
correctly and for every part of the task.
99
00:06:56,780 --> 00:06:59,510
Bob may ask for the help of any of them.
100
00:07:00,110 --> 00:07:01,580
Ask from their first guy.
101
00:07:01,600 --> 00:07:07,280
For example, to print a nice, good looking girl reading message through the screen to every user.
102
00:07:07,670 --> 00:07:09,830
And Mike will calculate the average grade.
103
00:07:09,860 --> 00:07:12,470
And Steve will print this rate.
104
00:07:12,680 --> 00:07:18,740
But Bob will not be in charge of all of the code inside of some main block.
105
00:07:18,950 --> 00:07:23,120
It will be divided to specialists, to some functions.
10553
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.