Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So functions are.
If you've ever heard the term functions, chances are you probably had some bad dreams about it.
Usually students find this term hard to grasp at first or second or ever.
They usually do not thoroughly understand this topic, but somehow managed to move on.
And since in programming, we have a lot of things related to one another.
Students who act the understanding of the functions topic tend to fill these gaps in their knowledge,
and they will feel difficulties with most of the topics that will follow afterward.
That's why I recommend and I think it's better to learn it once and learn it well and only then.
Once you've done it, you are allowed to proceed to more advanced sections and sections that you are
going to use functions in your programming language.
So in this section, the goal is to do our best to understand what are these functions?
Where do they come from?
Why do we need them for?
And why shouldn't we be so afraid of them?
I will try to give you an overview of real life situations that resemble in somewhat to how functions
are being used in programming.
And only once you get a hold of why we may be interested in using functions in general only then you
may consider moving on to use functions as part of your code in your programming language.
So here we go.
Grab a cup of your favorite drink, take a comfortable posture and let's go.
So I want you guys to think about a situation where we have Bob trying to develop his own newest and
coolest application that should address students needs and show them important information regarding
their grades.
So we can see here, Bob and Bob is there all alone in this story.
And he's trying trying his best to build this application.
But there are so many aspects that Bob needs to take care of, like getting grades from the users,
calculating every student's grade, printing every user's information to the screen.
And there is a lot of other students and there is a lot of students.
There is a lot of things that Bob has to do.
So why am I saying that?
What?
Why do I even say that?
Bob has a lot of things to do and there is a lot of students and so on and so forth.
Basically, what I want you to do in this example is that I want you to think of Bob as the main function,
the main block we've used in the course all along.
So on one hand, it seems like Bob could take care of all these aspects by on his own and all the code
of getting grades, calculating the average grade and so on.
All of that is going to be under the main block.
Just write down a bunch.
A very large bunch of code in the main section and help that this program will work.
So that's pretty much what we are.
We're used to doing all the exercises so far.
Right.
We simply gave Bob to take care of everything because we said, like in this analogy, that Bob is just
like our main function.
Okay.
But the main question that should be asked here is whether this approach is correct or wrong.
Writing all the code under the main section is probably going to be very tedious.
And not only that, but we will also be able to see that there are many repetitive operations that we
do.
For example, calculating every student's grade or printing every user's information should be pretty
much the same for all users.
Right.
And I mean, it's it's kind of obvious that there is a lot of commands that are going on over and over
again, and they are pretty much the same.
So why should we inflate our program with a lot of repetitive lines of code?
It simply doesn't make any sense.
And also, if sometimes in the future, once you release your program, if you will have some bug and
that may happen, how will you track the bug?
There is only Bob.
And you should go over all of his commands, probably line by line and try to track down where is the
problem.
So that's also another reason why you shouldn't leave all the code in just Bob's hands.
That's why it shouldn't be only in one main section.
So for these reasons we've just mentioned so far and for many reasons, we are going to mention also
later on, for these reasons, we decided that we are going to use functions if we know that the process
of calculating the average grade for every student is pretty much the same.
Then we would like to create some guy, let's call him Mike for this example, that his goal is, for
example, to be in charge of getting some grades from Bob and calculating the average grade between
them.
So all these guy, all this Mike knows to do is simply to receive a.
Full of grades and then calculate the average grade and finally return the result to Bob in this example,
meaning that if we have to calculate the average grade for 10 people, then the calculation process
is going to be the same for all of them.
Right.
Mike will just sum up all the grades he has been given and then divide the sum by the total number of
grades.
That's how you'd calculate an average grade.
So we can say that Mike will be in charge just of this task.
Only he will not read inputs from the user and he will not print anything to the screen.
He's here just to help Bob to accomplish everything he needs to get started, to get calculate, to
get his program on track.
Bob will give Mike the grades of some user user one, for example, and then Mike will only calculate
the average grade and return the results to Bob.
That's all Mike will do.
Bob calls Mike and gives him a couple of grades.
Mike finds out the average and returns to Bob.
That's it.
So once again, if there is some repetitive process of doing almost the same task over and over again,
we would like to use some skilled person for doing these kind of tasks.
So for the overall task that we specified previously, there will be a couple of specialists.
The first one knows how to print a greeting message to the to the screen and on the second one knows
how to calculate the average grade.
That's our mike.
And the third one will know we'll be capable of printing the average grade nudge, calculating the average
grade, but printing the average grade through the screen and so on.
So there may be a lot of people with a lot of specialties that will help Bob to organize his application
correctly and for every part of the task.
Bob may ask for the help of any of them.
Ask from their first guy.
For example, to print a nice, good looking girl reading message through the screen to every user.
And Mike will calculate the average grade.
And Steve will print this rate.
But Bob will not be in charge of all of the code inside of some main block.
It will be divided to specialists, to some functions.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.