Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle."— Presentation transcript:

1 Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle

2 Parameters and Arguments Parameter (also called a formal parameter) Parameter (also called a formal parameter) Used in a method definition as a placeholder for a value that will be substituted for it when the method is called Used in a method definition as a placeholder for a value that will be substituted for it when the method is called Visible only within the method definition Visible only within the method definition Argument (also called an actual parameter) Argument (also called an actual parameter) The value that is substituted for the placeholder The value that is substituted for the placeholder Visible in the part of the program that called the method Visible in the part of the program that called the method

3 Sample Java Method static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase Function definition (i.e., the function itself) Parameter Local variable The method shown below is from the DetermineLetterCase.java program. The method shown below is from the DetermineLetterCase.java program. DetermineLetterCase.java

4 Sample Method Invocation The method invocation shown below is from the DetermineLetterCase.java program. The method invocation shown below is from the DetermineLetterCase.java program.DetermineLetterCase.java class DetermineLetterCase { public static void main (String[] args) { // The code preceding if-else statement has been omitted // The code preceding if-else statement has been omitted // Report whether the letter is lower- or uppercase if (isLowerCase(myChar)) out.writeln("\nThe letter " + myChar + " is lowercase."); else out.writeln("\nThe letter " + myChar + " is uppercase."); // Report whether the letter is lower- or uppercase if (isLowerCase(myChar)) out.writeln("\nThe letter " + myChar + " is lowercase."); else out.writeln("\nThe letter " + myChar + " is uppercase."); // end main // end main } // end DetermineLetterCase class Argument

5 What Happens When a Method Expecting a Parameter Is Invoked? (i.e., called) The argument is passed to the method. The argument is passed to the method. The method initializes the parameter to the value of the argument. The method initializes the parameter to the value of the argument. The method executes the code contained within its body, optionally returning a value. The method executes the code contained within its body, optionally returning a value. The program resumes after the method invocation. The program resumes after the method invocation.

6 Sample Program Let’s revisit the sample program that uses this method: Let’s revisit the sample program that uses this method: DetermineLetterCase.java DetermineLetterCase.java DetermineLetterCase.java

7 Local Variables and Scope A variable declared within a block (a set of Java statements enclosed in curly braces) is local to that block. A variable declared within a block (a set of Java statements enclosed in curly braces) is local to that block. In other words, it exists only within the block in which it was declared (its scope); it does not exist outside the block. In other words, it exists only within the block in which it was declared (its scope); it does not exist outside the block. Example: Example: The variable isLower declared in the isLowerCase method (slide 3) exists only within that method (its scope); it is invisible to the main part of the program. The variable isLower declared in the isLowerCase method (slide 3) exists only within that method (its scope); it is invisible to the main part of the program.

8 Local Variables and Scope Declaring a variable within the initialization part of a for loop presents a similar problem. Declaring a variable within the initialization part of a for loop presents a similar problem. Let’s look at sample program ScopeDemo1.java to see why. Let’s look at sample program ScopeDemo1.java to see why.ScopeDemo1.java

9 Local Variables and Scope Let’s look at another sample program, ScopeDemo2.java, that demos additional concepts related to local variables and scope. Let’s look at another sample program, ScopeDemo2.java, that demos additional concepts related to local variables and scope. ScopeDemo2.java


Download ppt "Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle."

Similar presentations


Ads by Google