Prev Main Next

Creating Functions That Act Like Statements

Define a function that acts like a statement just as you would a function that acts like an expression, with two exceptions.

  1. The return type is void.

  2. You do not need to include a return statement. If you want to, you can write

         return;
      
    to exit the function immediately.


Example

  //=========================================
  // printGreeting(name) prints a greeting to
  // someone called name.
  //=========================================

  public static void printGreeting(String name)
  {
    System.out.println("Hello, "
                       + name
                       + ", how are you?");
  }

Prev Main Next