Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.

Similar presentations


Presentation on theme: "COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4."— Presentation transcript:

1 COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4

2 Breaking Problems Down () We commonly break large problems into smaller pieces –How do you put a puzzle together? Locate the perimeter pieces –Edges –Corners Separate the remaining pieces into smaller groups –Major colors and patterns –Common shapes –We follow a similar process when developing a program Split the program’s functionality into smaller sections Determine what data communication occurs between the sections –The data each section requires –The data each section returns Organize the sections logically Code each section one at a time –Start with the main function –Continue with the remaining sections »In logical order

3 Methods() Every Java program has 1 or more functions –1 method must be named “main” –Each method represents a logical section of the program –Method operation The main method is executed A statement of the main method calls another method –Execution of the program moved to the called method Every statement in the called method is executed –Following a top-down approach –If a statement calls another method »Execution moves to the called method After the last statement of the called method is executed –Execution of the program returns to the calling method »The statement immediately following the calling statement is executed The last statement of the main method is last statement executed –Unless execution is stopped earlier »A runtime error »An exit statement is executed

4 Data type of the returned value User-defined Munctions () General design of a method public int calcArea(int length, int width){ int area; area = length * width; return area; // Not required for void data type } Method name Parameter list (0 or more variables) Method body public int calcArea(int length, int width) The method’s header Method’s visibility

5 User-defined Functions () Important notes about method –A method can have any number and type of parameters (variables) A method without any parameters will always produce the same results –The return statement is required for all methods Except in the case of the void return type –A return statement can still be used »It must return “void” »i.e., return void; return; –The returned value must match the specified return type

6 Calling Methods () Calling a method requires the following –The name of the method –A value for every parameter in the method Values must be listed in the proper order Values must match the specified data type –The returned value may be assigned to a variable Not required, but the data will be lost public static void main(String[] args) { int myArea; myArea = calcArea(4, 6); calcArea(2, 3); } public static int calcArea(int length, int width) { int area; area = length * width; return area; }

7 Variable Scope Scope = Accessibility A variable’s scope is dependent on its location in the program –Global Scope Must be declared outside any methods –Usually in the global section of the class Global scope variables can be accessed from any method in the class –This can lead to unexpected results if the programmer is not careful Global scope variables are accessible as long as the class exists –Local Scope Must be declared inside a method –Includes parameter variables Local scope variables can only be accessed from within their method Local scope variables can only be accessed after they are declared A local scope variable can “hide” a global scope variable –This can lead to unexpected results if the programmer is not careful –Can be avoided by always using unique names for all variables

8 Variable Scope Examples public class ScopeTest { private static int area = 0; private static int len = 5; private static int wid = 3; public static void main(String[] args) { int myArea; myArea = calcArea(4, 6); calcArea(2, 3); } public static int calcArea(int length, int width) { int area; area = length * width; return area; }


Download ppt "COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4."

Similar presentations


Ads by Google