Java Basics - Functions 1

Function

Function allow us to group together block of code and allow us to do a bunch of tasks and reuse it
Calling a function is equivalent to executing the code in the definition.

Example of the println function.

public void println(String x){
    //check that the String exists
   if (x== null){
           x= "null";
   }
   try {
   ensureOpen();
   //write String to screen
    textOut.write(x);
   }catch(IOException e) {
         trouble = true;
   }
   newline();
   }

Function call of the above example --> System.out.println("Print this:);

Functions allow the following:
-Easy to reuse code
-Organize and group code
-More maintainable code.
_____________________________________________________________

The word public is an access modifier and anyone can use public functions.
The word void is a return type that does not return data and only performs actions eg printing text.
In the case above the println is the function name.

Comments

Popular posts from this blog

Java Basics - Variable and Data Types Part 1

Java Basics - Variable and Data Types Part 2

OOP - Objects and Classes