1 / 40

Methods

Methods. Yazd University, Electrical and Computer Engineering Department Course Title: Advanced Programming. Motivations. Method : groups statements that perform a function. Level of abstraction (black box) Code Reuse – no need to reinvent the wheel!.

Télécharger la présentation

Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods Yazd University, Electrical and Computer Engineering Department Course Title: Advanced Programming

  2. Motivations Method : groups statements that perform a function. • Level of abstraction (black box) • Code Reuse – no need to reinvent the wheel!

  3. A method is a collection of statements that are grouped together to perform an operation. Defining Methods

  4. Method signature is the combination of the method name and the parameter list. Method Signature

  5. The variables defined in the method header are known as formal parameters. Formal Parameters

  6. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. Actual Parameters

  7. A method may return a value. If the method does not return a value, the returnValueType is the keyword void. (i.e., main method) Return Value Type

  8. animation Calling Methods, cont.

  9. animation Trace Method Invocation i is now 5

  10. animation Trace Method Invocation j is now 2

  11. animation Trace Method Invocation invoke max(i, j)

  12. animation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2 Trace Method Invocation

  13. animation Trace Method Invocation declare variable result

  14. animation (num1 > num2) is true since num1 is 5 and num2 is 2 Trace Method Invocation

  15. animation result is now 5 Trace Method Invocation

  16. animation Trace Method Invocation return result, which is 5

  17. animation return max(i, j) and assign the return value to k Trace Method Invocation

  18. animation Execute the print statement Trace Method Invocation

  19. CAUTION A return statement is required for a value-returning method. The method shown below in (a) is logically correct, but it has a compilation error because the Java compiler thinks it possible that this method does not return any value. To fix this problem, delete if (n < 0) in (a), so that the compiler will see a return statement to be reached regardless of how the if statement is evaluated.

  20. Call Stacks

  21. animation Trace Call Stack i is declared and initialized

  22. animation Trace Call Stack j is declared and initialized

  23. animation Trace Call Stack Declare k

  24. animation Trace Call Stack Invoke max(i, j)

  25. animation Trace Call Stack pass the values of i and j to num1 and num2

  26. animation Trace Call Stack pass the values of i and j to num1 and num2

  27. animation Trace Call Stack (num1 > num2) is true

  28. animation Trace Call Stack Assign num1 to result

  29. animation Trace Call Stack Return result and assign it to k

  30. animation Trace Call Stack Execute print statement

  31. Overloading Methods Two methods can have the same name but different argument lists. Java examines the signatures to determine which method to call.

  32. Ambiguous Invocation • Two or more possible matches for an invocation of a method • Compiler cannot determine the most specific match. • Compilation error.

  33. Ambiguous Invocation public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } }

  34. Scope of Local Variables Local variable: defined inside a method. Scope: part of program where variable can be referenced. The scope of a local variable: from declaration until end of the block that contains it

  35. Scope of Local Variables, cont. Can you declare a local variable with the same name multiple times in a method? If the statements are in different non-nesting blocks of code

  36. Scope of Local Variables, cont. Variable declared in the initial action part of for loop: has scope in the entire loop Variable declared inside for loop body: has scope from its declaration to the end of the block

  37. Scope of Local Variables, cont.

  38. Scope of Local Variables, cont. // Fine with no errors public static void correctMethod() { int x = 1; int y = 1; // i is declared for (int i = 1; i < 10; i++) { x += i; } // i is declared again for (int i = 1; i < 10; i++) { y += i; } }

  39. Scope of Local Variables, cont. // With errors public static void incorrectMethod() { int x = 1; int y = 1; for (int i = 1; i < 10; i++) { int x = 0; x += i; } }

  40. Method Abstraction You can think of the method body as a black box that contains the detailed implementation for the method.

More Related