Lec5 Methods Array Basics

Lec5 Methods Array - Lecture 5 Announcements EE422C Data Structures Exam 1 Today re-visit java.lang.String method and array basics Next time OO

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 EE422C Data Structures Sarfraz Khurshid Lecture 5 9.9.10 Lecture 5 Announcements Exam 1 : Sep 30 2010 Today • re-visit java.lang.String • method and array basics EE322C 2 Next time • OO basics Methods allow us to modularize and encapsulate the processing of our programs. This is key to handling complexity. A method is a module in our program Method Mechanics EE322C 3 Defining Static Methods A static method operates on data passed through the parameter list, and returns a value computed or causes some mutation to occur or both. It may be called by name from other methods. The definition of a method consists of a header and a body inside of a defined class. Method Header • Has an access scope modifier (e.g. public) • Has a static modifier EE322C 4 • Has a return value type (such as double or void) • Has a method name (such as computeTax) • Has a list of formal parameters (such as double amount) • a parameter is a local variable declaration for an argument value to be sent into the method from the calling statement Method body { // the set of instructions that defines the implementation } the max method public static int max (int num1, int num2) { // find which of 2 integers is the largest int result = 0; method heading formal parameters method name return type modifiers EE322C 5 if (num1 > num2) result = num1; else result = num2; return result; } method body return value Example method call int larger = max (4, 8); actual arguments Parameter Passing In Java all arguments are passed to the formal parameters by value. The actual argument values are copied into the formal parameter variables when the method is called. They cannot be modified in the called method. 4 Actual arguments in the method call statement are Example method call int larger = max (4, 8); EE322C 6 8 4 8 num1 num2 and then the values are copied into the formal parameter variables in the method definition header evaluated (they can be expressions)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Functions and Procedures Methods that are non-void must return a value of the type indicated. These are similar to functions in some languages. Those that do not return a value (i.e. void) are called procedures in some languages. public static int max (int num1 int num2) The function max EE322C 7 public static int max (int num1, int num2) { // find which of 2 integers is the largest int result = 0; if (num1 > num2) result = num1; else result = num2; return result ; } Called as, e.g.: int x = max (4, 8); Functions and Procedures Methods that are non-void must return a value of the type indicated. These are called functions in other languages. Those that do not return a value (i.e. void) are called procedures in other languages. public static
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/02/2010 for the course EE 322C taught by Professor Nettles during the Spring '08 term at University of Texas at Austin.

Page1 / 7

Lec5 Methods Array - Lecture 5 Announcements EE422C Data Structures Exam 1 Today re-visit java.lang.String method and array basics Next time OO

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online