Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modules (Methods) Functions and Procedures Parameters...Output.

Similar presentations


Presentation on theme: "Modules (Methods) Functions and Procedures Parameters...Output."— Presentation transcript:

1 Modules (Methods) Functions and Procedures Parameters...Output

2 Procedures and Functions Some of our pseudocode rules translate into firm rules in Java: –Functions return a single value Value returned can be any complex data type (including an Object) –Procedures can “return” information by changing an object that is passed in via a parameter. Other rules are style issues: –Functions having no side effects –Functions not doing I/O

3 Java Methods Single construct for both procedures and functions: When a function is called for, specify the appropriate return type before the method name public float average (float fNum1, float fNum2) { float fReturnVal; fReturnVal = (fNum1 + fNum2)/ 2; return fReturnVal; } // of average to specify a procedure, make the return type void (More later…)

4 Writing Methods A Java requirement: --All methods belong to an object (or class). --Name of object (or class) must be unambiguous when the method called. --To run a program, there must be a class (whose name is the name-of-the-program), containing a special method called main: a class method, not an instance method visible to all nothing returned Method name for command line parameters public static void main (String[ ] argv)

5 Method Overloading Sometimes more than one method is required to do the same job. In Pseudocode, we frequently used a helper module Procedure Convert(Lhead iot in Ptr toa LLNode) Procedure ConvertHelper(Lhead iot Ptr toa LLNode, NewHead iot Ptr toa LLNode) In Java, multiple methods can have the same name as long as the number, type or order of their parameters is different public void Convert(LLNode Lhead) public void Convert(LLNode Lhead, LLNode NewHead)

6 Method Signatures “The signature of a method consists of the name of the method and the number and types of formal parameters to the method. A class may not declare two methods with the same signature, or a compile time error occurs.” Java Language Specification s.8.4.2 Method overloading occurs where identically named methods have different parameter (number, type or order NOT name!) public int getCube(int iNum){ return iNum * iNum * iNum; } public int getCube(float fNum){ return (int)(fNum * fNum * fNum); } public int getCube(double dNum){ return (int) (dNum * dNum *dNum); }

7 Methods: Common Mistakes public float average (float fNum1, float fNum2, float fNum3); { float fReturnVal; fReturnVal = (fNum1 + fNum2 + fNum3)/ 3; return (fReturnVal); } // of average Note ending semicolon -- results in unhelpful error message

8 Parameters inJava only has in parameters Initially this will appear to be a big change. It is a common feature in many languages. We can get information out of a module via two techniques –Return something from a function –Pass in a reference to some object and inside the method modify the object [side effect]

9 Printing to Screen Pseudocode: print ( ) Java: System.out.print( ); System.out.println( ); Inserts a newline after the printing is complete

10 Printing to Screen int x = 10; System.out.println(5); System.out.println( ); System.out.println(“Hello World”); System.out.println(“x = “ + x); System.out.println(“y = “ + 5); System.out.print(“All on one “); System.out.println(“line”); 5 Hello World x = 10 y = 5 All on one line

11


Download ppt "Modules (Methods) Functions and Procedures Parameters...Output."

Similar presentations


Ads by Google