Simple Example

5 minutes
Share the link to this page
Copied
  Completed
In this sense, a function is a type of procedure or routine. Some programming languages make a distinction between a function, which returns a value, and a procedure, which performs some operation but does not return a value. Most programming languages come with a prewritten set of functions that are kept in a library.

Transcript

Hello, in this video, we are going to take a look at method. So a method is just a way of abstracting code out. And just you know, putting it together usually put common code together, wrap it in a method, provide some meaningful name as a lunch calling again and again, you know, in your application. So, this is a method right here. I mean, this is the main method that is called when you first launch up your application. So you need one of the lucky use methods when we you know, you stuff to the strings and various other properties as well.

So if you watch the streams or the character videos, then you will probably remember doing method like, you know, to oppa. So like changing all the characters to a pad, so that was actually a method. We didn't implement it, but it actually did something to implement a method is actually really simple. All you got to do is put it up Towards the keyword public and we'll go into you know, different keywords for this. But in terms of you know, knowing how methods work not really that important, this is more when you get on to creating your own classes at the moment we're just using the default one. Next one the keyword static.

Again, this was gonna glance over this simply because it's going to be useful to explain what it is when we get on to actually making our own classes. So for now, just consider this as a necessity. So the first part is going to be your return type for the data that there is returning void like main, then he won't return anything so I'm gonna call this function one. So that's no very well men enough to go make sure you make functional well named. And in here for the system, dot out, dot print line to say for print It doesn't print me most thinking, well, what if, because this code is in a method until this method is called, so what the code inside of it will not run. So only in the activities function.

So I'm gonna, there we go, it's a fine. So let me show you the other type of function so public void so instead of void, we're gonna call it something else there. You can pretty much use any data type. So it's going to keep this in for you then add on function like so. Okay, and so be moaning. You'll say that it must have a return type and you can add the returns there and but that doesn't matter at the moment.

We can, you know, will return our own returns thing that we do return For example, so we'll return in just a value of six, obviously, you will probably have something a bit more than returning a single value, probably do some sort of calculation based on what the application doing. But the principle is like this, this is how you create a method. You can put whatever code you want in here. If you have a return tagless, no void, you can return some data, some information of that data type. So if we go here, now put return function. I'm not sure why isn't it you know, nothing Come on in the content is because this is one we print here.

There's no print here. You could add one, but on this demonstrating the return statement. And when this is caught at this moment, it's the same thing as watching now, obviously, that will run but it's the same thing as just having no the number 16 it does nothing. Whereas if we create In the variable, a in our assigned to return function on the System dot out dot print line. And I actually put in it here we get six, we could do some sort of calculations, lesson six times two, we get 12, maybe x equals eight and then plus eight to it. So it'd be six times two, which is 12 plus eight, which will be 20. x on them, and put there it prints out 20.

Now, so I think that is probably for the basics of functions during need to know much more than that just to get going. But over the next few videos, we'll cover the few excellent things that you can add to sort of make your function more useful. If you have any questions. Feel free. Reach out, and I look forward to seeing you in the next awesome Java video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.