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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Looking inside classes Fields, Constructors & Methods Week 3.
Programming Languages and Paradigms The C Programming Language.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 7: User-Defined Methods
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Understanding class definitions Looking inside classes.
Introduction to Programming with Java, for Beginners Scope.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
COMP More About Classes Yi Hong May 22, 2015.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Comments are for people Header comments supply basic information about the artifact.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
CPS120: Introduction to Computer Science Decision Making in Programs.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Expressions Methods if else Statements Loops Potpourri.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Methods.
01/24/2005 Introduction to Programming with Java, for Beginners A Closer Look at Constructors and Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Exam 2 Review.
Chapter 6: Methods CS1: Java Programming Colorado State University
Programming Fundamentals Lecture #7 Functions
Lecture 14 Writing Classes part 2 Richard Gesick.
Starting Out with Java: From Control Structures through Objects
Group Status Project Status.
CS2011 Introduction to Programming I Methods (II)
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Classes, Objects and Methods
Methods/Functions.
Corresponds with Chapter 5
Parameters and Arguments
Presentation transcript:

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

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

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

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

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.

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

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.

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

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