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
Alright here's an exercise for you
I want you to write a function that takes two numbers and returns
the maximum of the two. Call that function, give a different
argument, and make sure it's working properly. So pause the video,
do this exercise, and when you're done come back and continue watching.
I'm going to start by defining a function, call it max,
here we need two parameters, we can call them number 1, and
number2, or we can use shorter names like
a, and b, now earlier I told you not to use
mysterious variable names like a and b, but in this particular case
doesn't really matter, because a and b are kind of self explanatory, so we are not
dealing with a complex logic, we don't have to guess what a and b
are as you will see in a second. So in this function,
you want to have some logic, we want to compare the value of a with b, so we're
gonna use an if statement, so if
a is greater than b, we want to return
a. else you want to return b.
That's the simplest implementation, it's not the best way, we're going to optimize
this step by step, but before going any further, let's make sure
that this function actually works. So I'm going to declare
a variable called number and set it to max of
1 and 2. Now let's display number on the
console. Save the changes, so
max is 2, so if the second argument is greater,
our function works, what if the first argument is greater, so
let's change 1 to 3, save, now we see 3,
perfect, what if both arguments are equal,
once again, save the changes, we still see 3, perfect,
now before going any further, did you notice how I called this function
with different arguments, I called it with different test cases.
First I assume that the second argument is greater,
then I assume if the first argument is greater, and finally I assume
that both arguments are equal. So when writing code, when writing functions,
you should test your functions with different possible values, okay?
Now, let's get back to this max function and clean up this code.
But first thing I want to improve here is to remove
this else keyword. Why? Because of 8 is greater
than b, we're going to return a, so we'll jump out of this function.
So none of the code after line 6 will be executed, in fact,
it will never get to this point, so we don't really need the else
keyword. Okay? So if a is greater than b, we'll
return a, otherwise we'll return b. So this is a
cleaner implementation. But hey we can make this even cleaner. So earlier
we learned about the conditional operator. Remember this?
So we add a condition in parenthesis.
Question mark, if this question evaluates to true,
we use 1 value, otherwise we use the other value.
We can rewrite these two lines, using our conditional operator
so what is the condition here? If a is greater than b.
If this condition is true, we want to return a,
otherwise we want to return b. All we need here is
a return statement and terminate this with a semi colon.
So, this line is exactly equivalent to these two lines.
Now, save the changes,
both arguments are equal, you get 3, so far so good,
let's try another test case. I'm going to make the first argument
larger, let's say 5, we get 5,
now let's make the second argument larger, 10, beautiful.
So our function is working.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.