|
With Functions, we can execute codes over and over again so , we don't have to write same code again |
As you know, firs we need a java file, a class and main method
There is an example class and main method
We are going to write our function in the class, but outside the main method
Until now, we have learned access keyword and return type keyword
We can write "public void" , But there is one more keyword
Now the last keyword we should write is name of the functions
Example function = public void print
And last, we need to put two parentheses and curly braces
To call a function, you should write it's name and put "()"
It will do it's job
Note : If you write your function in your main class it should have static keyword
"public static void print()"
Static is also a keyword, it means you can call this function in this class, without creating a object
We did not learn classes and object yet
We have just learned void type functions, that returns nothing
Now we are gonna look at functions with return type
First, we need to write a return type instead of "void"
Example function : public static int add(){}
You can give variable to function
Function can use this variable and make it's job
As you remember, when we write a function, we add parentheses "()"
In this parentheses, we can declare a variable like "int number, String text"
And when you call a function, you must enter these variables to function