Methods Scope How are names handled?

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

CPSC 388 – Compiler Design and Construction
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.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
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 Functions II Instructor: Mohammad Mojaddam.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Computer Science 1620 Function Scope & Global Variables.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Introduction to Programming with Java, for Beginners Scope.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
The different kinds of variables in a Java program.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
C++ for Engineers and Scientists Third Edition
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
ECE122 Feb. 22, Any question on Vehicle sample code?
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
Copyright © – Curt Hill Pointers A Light Introduction.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
Object Oriented Programming Criteria: P2 Date: 07/10/15 Name: Thomas Jazwinski.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © Curt Hill Flow of Control A Quick Overview.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
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.
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
Flow of Control An Overview
Chapter 7: User-Defined Functions II
The Lifetime of a Variable
Department of Computer Science
The for-each or for-all loop introduced in Java 5
Classes & Objects.
Function There are two types of Function User Defined Function
Chapter 5: Loops and Files.
Programming Fundamentals Lecture #7 Functions
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods.
Using local variable without initialization is an error.
Methods Additional Topics
Defining Classes and Methods
Doing things more than once
Scope of Variables.
Chapter 6 Methods: A Deeper Look
More On Enumeration Types
Arrays in Java What, why and how Copyright Curt Hill.
CHAPTER 6 GENERAL-PURPOSE METHODS
Compound Statements A Quick Overview
CS2011 Introduction to Programming I Methods (II)
Scope Rules and Storage Types
Methods Again Parameter Passage
Making something known
Tonga Institute of Higher Education
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Observing how the machine acts
The Java switch Statement
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
The Three Attributes of an Identifier
Corresponds with Chapter 5
The beginning of media computation Followed by a demo
Scope Rules.
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

Methods Scope How are names handled? Copyright © 1997 - 2009 Curt Hill

Recall … A method consists of A return type A name A parameter list A compound statement Each compound statement creates a new scope block What is scope? Copyright © 1997 - 2009 Curt Hill

Duplicate Names Java like the C family may safely use duplicate variable names Not just overloaded method names They must have different scope They may have different types The scoping of variables and how the compiler handles it has some complications We must examine this carefully Copyright © 1997 - 2009 Curt Hill

What is Scope? The scope of a variable is those lines where the variable is known Also applies to constants, classes and other named items The scope of a variable starts at the line of its declaration It continues on until the ending of the enclosed compound statement Copyright © 1997 - 2009 Curt Hill

Example int sqr(int val){ int temp = val * val; return temp; } int cube(int x){ int temp = x * x * x; No conflict between the two temp variables since they are in different scope blocks Copyright © 1997 - 2009 Curt Hill

Scope The compound statement forms a new scope block The method parameters are part of this block The Java rule is that any new name may be used The scope of two variables may not overlap The method may access any instance variables or static variables of the class Copyright © 1997 - 2009 Curt Hill

Scope Again No code outside the method may look into the method, but the method may access things outside the method Parameters are half way in between Any code that can see the method may see the number and type of the parameters The names are only known inside the method Copyright © 1997 - 2009 Curt Hill

Duplicates How does a program handle duplicates? When a variable name is found Compiler checks current block If found it stops If not it steps into next larger block and checks again When we are done we have found it or complained it is not there Copyright © 1997 - 2009 Curt Hill

Sample Functions int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Scope Blocks Drawn int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Look for val int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Look for val int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Look for y variable int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Not local, must be global int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Look for z int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Not found – error int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © 1997 - 2009 Curt Hill

Duplicate names Since a name is confined to a compound statement duplicate names are acceptable in some cases Cannot define the name twice in same scope Now the most important thing for naming a variable is not whether it has been used before, but a meaningful name Copyright © 1997 - 2009 Curt Hill