Department of Computer Science

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
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.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Department of Computer Science
LAB 10.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Slides by Donald W. Smith
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
1 Conditionals Instructor: Mainak Chaudhuri
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
COM S 228 Algorithms and Analysis Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Classes - Intermediate
Methods.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
CS001 Introduction to Programming Day 6 Sujana Jyothi
1 Conditionals Instructor: Mainak Chaudhuri
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Functions + Overloading + Scope
Chapter 2 Clarifications
AKA the birth, life, and death of variables.
Practice + Method Xiaozhong Liu
C Functions -Continue…-.
ㅎㅎ Fourth step for Learning C++ Programming Namespace Function
Department of Computer Science
Something about Java Introduction to Problem Solving and Programming 1.
Decision statements. - They can use logic to arrive at desired results
Computing Adjusted Quiz Total Score
Variables and Their scope
Group Status Project Status.
CS150 Introduction to Computer Science 1
AKA the birth, life, and death of variables.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
class PrintOnetoTen { public static void main(String args[]) {
if-else if (condition) { statements1 } else { statements2
Scope of variables class scopeofvars {
while while (condition) { statements }
Building Java Programs
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Suggested self-checks:
6.2 for Loops Example: for ( int i = 1; i
Methods (a.k.a functions)
Methods Scope How are names handled?
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Department of Computer Science COM S 207 Variable Scope Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu

The scope of a variable is the part of the program in which it is visible public static void main(String[] args) { System.out.println(cubeVolume(10)); } public static double cubeVolume(double sideLength) return sideLength * sideLength * sideLength; the scope of the parameter sideLength is the entire cubeVolume method, but not the main method

Local variable: A variable that is defined within a method public static void main(String[] args) { int sum = 0; for (int i=0; i<=10; i++) int square = i * i; sum = sum + square; } System.out.println(sum); The scope of a local variable ranges from its declaration until the end of the bloack or for statement in which it is declared.

An example of a scope problem public static void main(String[] args) { double sideLength = 10; int result = cubeVolume((); System.out.println(result); } public static double cubeVolume() return sideLength * sideLength * sideLength; trying to reference a variable defined outside of the method body

The same variable name may be used more than once public static void main(String[] args) { int result = square(3) + square(4); System.out.println(result); } public static int square(int n) int result = n * n; return result;

The same variable name may be used more than once public static void main(String[] args) { int result = square(3) + square(4); System.out.println(result); } public static int square(int n) int result = n * n; return result;

You can have two variables with the same name in the same method public static void main(String[] args) { int sum = 0; for (int i=1; i<=10; i++) sum = sum + i; }

Illegal: two variables with the same name have their scopes overlapped public static int sumOfSquares(int n) { int sum = 0; for (int i=1; i<=n; i++) int n = i+1; sum = sum + n; } return sum;

Exercise what is the scope of variable i defined in line 13? public class Sample { public static void main(String[] args) int x = 4; x = mystery(x+1); System.out.println(s); } public static int mystery(int x) int s = 0; for (int i=0; i<x; x++) int x = i+1; s = s + x; return s; what is the scope of variable i defined in line 13? which is the scope of variable x defined in line 10? there are two scope errors in the code. what are they?