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
Hungarian
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
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
Now there is a
cleaner way to write this code on line 3, all this concatenations
are kind of ugly, they're getting in the way, later in the course I will show you
how to use template literals to clean up this code. For now,
don't worry about it, let's look at another example of a function.
This function we have here is performing a task. So,
Performing a task. This task is to display something on the
console. But sometimes our functions might calculate something,
so, here is an example of a function that calculates
a value. So again, function, let's call
this function square, this function should take a parameter, let's call
that number, now we need to calculate the square
of that number, that is number times number.
Just basic math. Right? Now we need to return this value
to whoever is calling this function. For that we use
the return keyword. That's another reserve keyword
so we cannot have a variable called return, okay? Now instead of
calling the greet function, let's call the square function. So,
square, we pass 2, now this returns a value
so we can use that value to initialize a variable.
For example, you can declare another
variable called number, and set it to a square of 2.
And then we can display that on the console.
Save the changes, so we get 4. Now,
in this particular example, we don't necessarily have to declare a separate
variable if all we want to do is display the square of 2
on the console. We can exclude this variable declaration,
and simply pass square of 2,
to console.log.
So, when the JavaScript engine executes
this code, first it's going to call this function, it will get a value
and then pass that value to console.log.
Save the changes and look we still get 4.
Now, I have a question for you. How many function calls do you think we
have in this code? We have 2 function calls.
Square of 2, is one function call, let me
delete this temporarily, but console.log is also
another function call, right? Because here we have parenthesis,
so, we're calling the log function, which is defined somewhere,
and passing an argument, we can pass a simple string,
like Hello, or we can pass an expression, that expression can be
a call to another function, like square of 2.
Okay? So this is the basics of functions. Again, later,
in the course we have a comprehensive section about functions, for now, all I want
you to take away, is that a function is a set of statements
that either performs a task, or calculates
and returns a value. A real world application is essentially
a collection of hundreds or thousands of functions working together
to provide the functionality of that application.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.