Methods intro-1.0

2,411 views
2,327 views

Published on

A brief introduction to methods in Java

Published in: Technology, Health & Medicine
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,411
On SlideShare
0
From Embeds
0
Number of Embeds
1,392
Actions
Shares
0
Downloads
108
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • * 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.* ##
  • * 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.* ##
  • * 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.* ##
  • * 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.* ##
  • Methods intro-1.0

    1. 1. Introduction Methods
    2. 2. What is a Method ? <ul><li>A method is a kind of building block that solves a small problem </li></ul><ul><ul><li>A piece of code that has a name and can be called from the other code </li></ul></ul><ul><li>Methods allow programmers to construct large programs from simple pieces </li></ul><ul><li>Methods are also known as functions , procedures , and subroutines </li></ul>
    3. 3. Why Use Methods? <ul><li>More manageable programming </li></ul><ul><ul><li>Better organization of the program </li></ul></ul><ul><ul><li>Easy to read programs </li></ul></ul><ul><li>Avoid repeating code </li></ul><ul><li>Code reusability </li></ul><ul><ul><li>Using existing methods several times </li></ul></ul>
    4. 4. Declaring and Creating Methods
    5. 5. Declaring and Creating Methods <ul><li>Each method has a name </li></ul><ul><ul><li>It is used to call the method </li></ul></ul><ul><ul><li>Describes its purpose </li></ul></ul>public static void printLogo () { System.out.println(&quot;FunSoft Corp.&quot;); System.out.println (&quot;www.funsoft.bg&quot;); }
    6. 6. Declaring and Creating Methods (2) <ul><li>Methods declared public can be called by any other classes </li></ul><ul><ul><li>This will be discussed later in details </li></ul></ul><ul><li>Methods declared static can be called by any other method (static or not) </li></ul><ul><ul><li>This will also be discussed later </li></ul></ul><ul><li>The keyword void means that the method does not return any result </li></ul>public s tatic void p rintLogo( ) { System.out.println(&quot;FunSoft Corp.&quot;); System.out.println (&quot;www.funsoft.bg&quot;); }
    7. 7. Declaring and Creating Methods (3) <ul><li>Each method has a body </li></ul><ul><ul><li>It contains the programming code </li></ul></ul><ul><ul><li>Surrounded by { and } </li></ul></ul>public static void printLogo() { System.out.println(&quot;FunSoft Corp.&quot;); System.out.println(&quot;www.funsoft.bg&quot;); }
    8. 8. Using Parameters Defining and Using Method Parameters
    9. 9. Method Parameters <ul><li>To pass information to a method, you can use parameters </li></ul><ul><ul><li>You can pass zero or several values </li></ul></ul><ul><ul><li>You can pass values of different types </li></ul></ul><ul><li>Use parameters to change the way the method works every time you call it </li></ul>
    10. 10. Defining and Using Method Parameters <ul><li>Method’s behavior depends on its parameters </li></ul><ul><li>Parameters can be of any type </li></ul><ul><ul><li>int , double , String , etc. </li></ul></ul><ul><ul><li>arrays ( int[] , double[] , etc.) </li></ul></ul>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. 11. Returning Values From Methods
    12. 12. Returning Values From Methods <ul><li>A method can return a value to its caller </li></ul><ul><li>Returned value: </li></ul><ul><ul><li>Can be assigned to a variable: </li></ul></ul><ul><ul><li>Can be used in expressions: </li></ul></ul><ul><ul><li>Can be passed to another method: </li></ul></ul>String message = input.nextLine(); // input.nextLine() returns a string float price = g etPrice () * quantity * 1.20; System.out.println( input.nextLine() ) ;
    13. 13. Creating a Method That Returns a Value <ul><li>Instead of void , specify the type of data you want to return </li></ul><ul><li>Methods can return any type of data ( int , String , array, etc.) </li></ul><ul><li>void methods do not return anything </li></ul><ul><li>Use return keyword to return a result </li></ul>public static int multiply(int number1, int number2) { return number1 * number2; }
    14. 14. return Statement <ul><li>return statement: </li></ul><ul><ul><li>Terminates method’s execution </li></ul></ul><ul><ul><li>Returns the given expression to the caller </li></ul></ul><ul><li>To terminate void method, simply write return; </li></ul><ul><li>You can use return several times in a method </li></ul>
    15. 15. Questions ? Methods – Introduction

    ×