Java

TOPIC 30 – VOID FUNCTIONS

 

 

LESSON NOTE

 

 

RETURN-TYPE OF VOID

 

We have already worked with return-types of type int, double and String.  We will now consider void.

 

Functions can have a return-type of void.  This simply means that the function will not return anything at all.  Such functions don’t even need a return statement. 

 

Once a void function is done being executed, Java jumps back to the location where the function was called.

 

PURPOSE

 

For now, the main purpose of a void function is probably to output information to screen.  However, in the future, we will use void functions a lot more in order to update and alter data.

 

VOID FUNCTION CALLS

 

Functions calls to void functions are a little different than other function calls because they do not return anything.  Therefore, the entire line is simply the function call. 

 

EXAMPLE – VOID FUNCTION IMPLEMENTATION

 

Create a void function called outputName that gets two String arguments called first and last and outputs the person’s full name with spacing.

 

     public static void outputName(String first, String last)

     {

        System.out.println(first + " " + last);

     }

 

EXAMPLE – VOID FUNCTION CALL

 

Assuming the above function is located in a class called Fun, make use of the function above.

 

            Fun.outputName("Patrick", "Campeau");