Variables and Their scope

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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)
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Written by: Dr. JJ Shepherd
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Road Map Introduction to object oriented programming. Classes
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Review of C++ Programming Part II Sheng-Fang Huang.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
Functions, Pointers, Structures Keerthi Nelaturu.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Topic: Classes and Objects
Examples of Classes & Objects
AKA the birth, life, and death of variables.
C Functions -Continue…-.
Functions, locals, parameters, and separate compilation
A Lecture for the c++ Course
Classes.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Programming Language Concepts (CIS 635)
Object-Oriented Programming Using C++
Using local variable without initialization is an error.
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Pointers and References
C Passing arrays to a Function
Scope, Parameter Passing, Storage Specifiers
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 11 C Parameters Richard Gesick.
Object Oriented Programming COP3330 / CGS5409
METHODS AND BEHAVIORS AKEEL AHMED.
User Defined Functions
Classes & Objects: Examples
Return by Reference CSCE 121 J. Michael Moore.
Object Oriented Programming
CS2011 Introduction to Programming I Objects and Classes
CS2011 Introduction to Programming I Methods (II)
AKA the birth, life, and death of variables.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming Review
Scope of variables class scopeofvars {
Method of Classes Chapter 7, page 155 Lecture /4/6.
Submitted By : Veenu Saini Lecturer (IT)
Chapter 9: Pointers and String
Methods/Functions.
Class Circle { Float xc, yc, radius;
Corresponds with Chapter 5
C Parameter Passing.
Presentation transcript:

Variables and Their scope All variables have a scope of existence The context in which they are accessible by a program The scope of a variable starts in the block of code in which it is declared, and propagates downward Similarly, different functions or subroutines at the same level have totally different scopes! But may both inherit access to class-wide variables { int a; a = 10; { int b; b = a; // a comes from the // higher block } a = b + 2; // error! }

Function Parameters For basic data types (boolean, int, double, etc.), when you pass data to a function or subroutine, the function actually gets a copy of the argument, under a different name public static int square(int x){ int s = x * x; return s; }

Function parameters A consequence of the above is that if you modify a function parameter, it is not reflected in the original copy public static void square(int x){ x = x * x; } square(a); // the value inside a does not get changed!

Objects Review: difference between static and non- static (aka dynamic)? Binding/Scope Review: Instance methods? How are the called? Instance variables? How are they accessed? Static methods: How are they called? Static variables:

Object allocation Walk through “memory on paper” example. class Inventory{ public String name; public int quantity; public double price; } The command “new Inventory()” allocates space for all three attributes, and gives them one name of the whole object

Object Usage Recall: objects are all by reference, meaning any subroutine that uses them as parameters has access to the actual objects, instead of a copy. Now, if we pass an object to a function, and that function makes a change to the data inside, it will affect the original! Can be good or bad, but has to be remembered

Object methods When you call a function that is a member of an object, that function has in its scope (in addition to any locally defined variables): Its parameters The instance variables of the parent object The (static) class variables of the parent object’s class Example: Student class with auto-populated ID’s