Introduction Methods
What is a  Method ? A  method  is 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 as  functions ,  procedures , and  subroutines
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
Declaring and  Creating  Methods
Declaring and  Creating  Methods Each method has a  name It is used to call the method Describes its purpose public  static void  printLogo ()   { System.out.println("FunSoft Corp."); System.out.println ("www.funsoft.bg"); }
Declaring and  Creating  Methods (2) Methods declared  public  can be called by any other classes This will be discussed later in details Methods declared  static  can be called by any other method (static or not) This will also be discussed later The keyword  void  means 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"); }
Declaring and  Creating  Methods (3) Each method has a  body It contains the programming code Surrounded by  {  and  } public  static void printLogo()   { System.out.println("FunSoft Corp."); System.out.println("www.funsoft.bg"); }
Using Parameters Defining and Using Method Parameters
Method  Parameters To pass information to a method, you can use  parameters 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
Defining and Using  Method Parameters Method’s 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(&quot;Positive&quot;); else if ( number  < 0) System.out.println(&quot;Negative&quot;); else System.out.println(&quot;Zero&quot;); }
Returning Values From Methods
Returning Values From Methods A method can  return  a 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() ) ;
Creating a Method That Returns a Value Instead of  void , specify the type of data you want to return Methods can return any type of data ( int ,  String , array, etc.) void  methods do not return anything Use  return  keyword to return a result public static  int  multiply(int number1, int number2) { return  number1 * number2; }
return  Statement return  statement: Terminates  method’s execution Returns the given expression to the caller To terminate  void  method, simply write  return; You can use return several times in a method
Questions ? Methods – Introduction

Methods intro-1.0

  • 1.
  • 2.
    What is a Method ? A method is 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 as functions , procedures , and subroutines
  • 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 and Creating Methods
  • 5.
    Declaring and Creating Methods Each method has a name It is used to call the method Describes its purpose public static void printLogo () { System.out.println(&quot;FunSoft Corp.&quot;); System.out.println (&quot;www.funsoft.bg&quot;); }
  • 6.
    Declaring and Creating Methods (2) Methods declared public can be called by any other classes This will be discussed later in details Methods declared static can be called by any other method (static or not) This will also be discussed later The keyword void means that the method does not return any result public s tatic void p rintLogo( ) { System.out.println(&quot;FunSoft Corp.&quot;); System.out.println (&quot;www.funsoft.bg&quot;); }
  • 7.
    Declaring and Creating Methods (3) Each method has a body It contains the programming code Surrounded by { and } public static void printLogo() { System.out.println(&quot;FunSoft Corp.&quot;); System.out.println(&quot;www.funsoft.bg&quot;); }
  • 8.
    Using Parameters Definingand Using Method Parameters
  • 9.
    Method ParametersTo pass information to a method, you can use parameters 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 Using Method Parameters Method’s 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(&quot;Positive&quot;); else if ( number < 0) System.out.println(&quot;Negative&quot;); else System.out.println(&quot;Zero&quot;); }
  • 11.
  • 12.
    Returning Values FromMethods A method can return a 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 MethodThat Returns a Value Instead of void , specify the type of data you want to return Methods can return any type of data ( int , String , array, etc.) void methods do not return anything Use return keyword to return a result public static int multiply(int number1, int number2) { return number1 * number2; }
  • 14.
    return Statementreturn statement: Terminates method’s execution Returns the given expression to the caller To terminate void method, simply write return; You can use return several times in a method
  • 15.
    Questions ? Methods– Introduction

Editor's Notes

  • #2 * 10/16/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #5 * 10/16/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #9 * 10/16/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #12 * 10/16/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##