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
A function performs a task.
So far, you learned to storing that information, using variables and control the way your code runs,
using offense, which.
The next step is to organize your code and the functions.
And in this lesson, you're gonna learn to write functions and call them.
So first, you're going to in a new job, a project named section for.
And inside the project, you're going to get a new file named chorused Java and make sure the chorus
class has the main method.
All right, in this lesson, you're going to build a program that sings the chorus to blame it on the
boogie by a Michael Jackson.
If you listen to the song, then you're familiar with the chorus.
The chorus is just four lines long and it's one of the more famous songs out there.
In any case, because the chorus is four lines long, that means we need for print statements.
So I'm going to print.
Don't blame it on the sunshine.
Don't blame it on the moonlight.
Don't blame it on goodtimes.
Blame it on the boogie.
And how robotic do I sound in any case, Michael Jackson sings this chorus five times, so your application,
the chorus singer, needs to do the same.
So naturally, I'm going to try to copy and paste this far more times.
All right, let's just run to make sure everything's all right.
We wrote a lot of code.
And everything's good.
But wow, that was so much code for a task, so simple.
Honestly, just looking at it makes me nauseous on that very same note, never, ever copy and paste
code ever.
It's very bad practice.
So how do we sing the chorus five times without copying the code five times?
Well, let me introduce functions.
If you ever feel the need to copy and paste code, then you should probably use a function instead because
the function is a grouping of code that can perform a task.
You're going to make a function that performs one task, and once you define the function, you can
call it as many times as you need to.
Now, there are five parts to a function, there is the level of access which is more often than not
going to be public because public means we can access the function from anywhere.
There's the return value in this case, void means the function doesn't return a value, it returns
nothing void.
The function name the name of the function should reflect the task that it performs, there are the
parameters, the function can receive parameters and use them in its code.
And then there's the code that actually performs your task.
And so we could talk about this all day, but the best way to understand the different function parts
is to employ them by creating your first function.
It's going to be a very simple function, the function is going to be public.
So that we can call the function from anywhere.
The return value, he was going to be void and it's not going to return any values.
The name of the function is going to be sing chorus because it's going to sing the chorus to the Michael
Jackson song.
And the function is not going to have any parameters because the code is not going to need them.
All right.
Now, remember that a function is a grouping of code that performs one task.
So it's going to contain code that sings the chorus.
All right, that's all you define a function that performs a task and it sings the chorus.
So how do we use this function?
How do we run its.
We need to call a function in order to run its.
The Syncora function is public, which means we can call it from anywhere, and so we need to call it
from Maine because Maine is the entry point of our application.
And calling a function is easy, all you need to do is refer to it by name saying Kooris.
With a set of parentheses and there seems to be an error.
Let's compile our code anyway, just to get a better picture of what that error really is.
And there is the error, non static method, Syncora cannot be referenced from a static context.
If you truly take the time to understand what this error is telling you, it says that we're calling
the Syncora function from the static main method.
And for this to work, the function that we're calling needs to be static as well.
In essence, all it's telling you is that you need to add static keyword to your function.
All right, things time to run the code.
And everything works out.
aFunction is a grouping of code that performs a task, can you define a function that sings Acorah's?
And then you called the function now, even though this worked, you might still have some questions
regarding the static Fuad.
For now, I'm going to tell you is that every function in our class needs to be static, don't worry
about why that is.
For now, you're going to learn more about the static keyword in module two object oriented programming,
because understanding how static works requires a basic understanding of programming using objects.
In any case, what's interesting here is how easy it is to call a function, if you want to sing the
chorus many times, then just call the function many times.
All right, we can run the code.
And that is just amazing, this is what makes functions reuseable.
Once you define a function, you can call it as many times as you want.
Functions make your code more organized than reuseable.
So go back to the same logic, but without functions, and this is extremely bad coding, if you ever
find yourself copying and pasting code, just stop.
It's a very bad practice.
Instead, make a function that performs the task and then you can call the function as many times as
you need to.
And to me, this looks a lot better.
Now, here's a fun fact for you.
Maine is also a function.
Look at Maine and course, they have the exact same signature public static void.
And that's because both of them are functions, but they both have separate tasks, the course function
performs the task of singing a chorus, and the main function performs the more important task of running
your application whenever you run your code.
What Java is actually doing is it's calling the main function behind the scenes.
And here are some things for you to consider, always right function names using lower Camiel case like
sing chorus, play game kickball.
The first word starts with a lowercase letter and the words that follow are uppercase.
And you're often going to hear functions being referred to as method's functions and methods mean the
exact same thing.
In this video, you learn to define a function and call it first, I showed you the dark and grim world
of coding without functions.
You built a program that sings the chorus to blame it on the boogie by Michael Jackson.
And the chorus is four lines long.
Michael sings the chorus five times.
So this demanded 20 print line statements.
Now, looking at this code is nauseating.
You should never copy and paste code instead make a function that performs the task and then call the
function as many times as you need to.
You created a function that sings the chorus and then you call the function as many times as you need
it to.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.