15
Introduction Introduction Methods Methods

Methods intro-1.0

Embed Size (px)

DESCRIPTION

A brief introduction to methods in Java

Citation preview

  • 1. Introduction Methods

2. What is aMethod ?

  • Amethodis a kind of building block that solves a small problem
    • A piece of code that has a name and can be called from the other code
  • Methods allow programmers to construct large programs from simple pieces
  • Methods are also known asfunctions ,procedures , andsubroutines

3. Why Use Methods?

  • More manageable programming
    • Better organization of the program
    • Easy to read programs
  • Avoid repeating code
  • Code reusability
    • Using existing methods several times

4. Declaring andCreatingMethods 5. Declaring andCreatingMethods

  • Each method has aname
    • It is used to call the method
    • Describes its purpose

publicstatic voidprintLogo () { System.out.println("FunSoft Corp."); System.out.println ("www.funsoft.bg"); } 6. Declaring andCreatingMethods (2)

  • Methods declaredpubliccan be called by any other classes
    • This will be discussed later in details
  • Methods declaredstaticcan be called by any other method (static or not)
    • This will also be discussed later
  • The keywordvoidmeans that the method does not return any result

public s tatic void p rintLogo( ){ System.out.println("FunSoft Corp."); System.out.println ("www.funsoft.bg"); } 7. Declaring andCreatingMethods (3)

  • Each method has abody
    • It contains the programming code
    • Surrounded by{and}

publicstatic void printLogo() { System.out.println("FunSoft Corp."); System.out.println("www.funsoft.bg"); } 8. Using Parameters Defining and Using Method Parameters 9. MethodParameters

  • To pass information to a method, you can useparameters
    • You can pass zero or several values
    • You can pass values of different types
  • Use parameters to change the way the method works every time you call it

10. Defining and UsingMethod Parameters

  • Methods behavior depends on its parameters
  • Parameters can be of any type
    • int ,double ,String , etc.
    • arrays ( int[] ,double[] , etc.)

public static void printSign( int number ) { if ( number> 0) System.out.println("Positive"); else if ( number< 0) System.out.println("Negative"); else System.out.println("Zero"); } 11. Returning Values From Methods 12. Returning Values From Methods

  • A method canreturna value to its caller
  • Returned value:
    • Can be assigned to a variable:
    • Can be used in expressions:
    • Can be passed to another method:

String message = input.nextLine(); // input.nextLine() returns a string float price =g etPrice ()* quantity * 1.20; System.out.println( input.nextLine() ) ; 13. Creating a Method That Returns a Value

  • Instead ofvoid , specify the type of data you want to return
  • Methods can return any type of data ( int ,String , array, etc.)
  • voidmethods do not return anything
  • Usereturnkeyword to return a result

public staticintmultiply(int number1, int number2) { returnnumber1 * number2; } 14. returnStatement

  • returnstatement:
    • Terminatesmethods execution
    • Returns the given expression to the caller
  • To terminatevoidmethod, simply writereturn;
  • You can use return several times in a method

15. Questions ? Methods Introduction