Lecture 4

advertisement
COP 2220
Computer Science I
Lecture 4
Topics
–
–
–
–
–
Breaking Problems Down
Functions
User-defined Functions
Calling Functions
Variable Scope
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
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
User-defined Munctions ()
• General design of a method
Method’s
visibility
Data type of
the returned
value
Method name
Parameter list
(0 or more variables)
public int calcArea(int length, int width){
int area;
Method
body
area = length * width;
return area; // Not required for void data type
}
public int calcArea(int length, int width)
The method’s header
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
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;
}
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
Variable Scope
• Examples
public class ScopeTest
private static int
private static int
private static int
{
area = 0;
len = 5;
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