Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
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 Functions II Instructor: Mohammad Mojaddam.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition P. 1Winter Quarter Daily Assignment A21 A21 asks.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm 2 Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
4.1 Instance Variables, Constructors, and Methods.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 26: FILING HANDLING in C Language FILING HANDLING in C Language-1 Lecture 26.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Week 11 Multi-file Programs and Scope of Variables.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Windows Programming Lecture 03. Pointers and Arrays.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
User-Written Functions
Bill Tucker Austin Community College COSC 1315
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Local Variables, Global Variables and Variable Scope
STORAGE CLASS.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
STORAGE CLASS.
Constructors & Destructors
Scope Rules.
Presentation transcript:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables Lecture 13

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 2Winter Quarter Scope of Variables The "scope" of a variable is the region within a program where that variable name is defined. A variable that is declared before any statements which define a function (such as "void main ( )" ) has file scope. That is, its name, its designated memory location, and any value assigned to it will be known throughout the main function and any other function (subprogram) in the same file of source code.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 3Winter Quarter Scope of Variables A variable that is declared immediately after the statements " void main { " will have the main program as its scope but will not be known outside the main function (for instance, not in function scanf). This is known as block scope. A variable that is declared inside a block of statements (inside or after the "{" ) will have only that block as its scope. It's scope ends at the "}". It is less confusing when all declarations either come before any functions (global variables) or only after the first "{" in a function (local variables).

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 4Winter Quarter Scope of Variables The same variable name can not be defined (declared) twice within the same block of code. A block of code is the code inside of a set of braces { }. However, a variable in C++ can be declared in other blocks of code (and even within nested blocks). If the same variable name is declared in multiple nested blocks of code, the declaration that is within the particular block of code that is being executed at that time is the one that is currently "in scope" or in effect.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 5Winter Quarter Scope of Variables Example The following program will not compile because x is only defined inside the if block of statements and is not known outside that block (neither before nor after it).

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 6Winter Quarter Scope of Variables /* This program doesn't compile, x is not declared */ #include void main ( ) { FILE *fptr ; fptr = fopen ("scope.dat", "w") ; fprintf (fptr, ”before if block, x=%d\n", x); if ( x == 3 ) /* x not declared yet -- error */ { float x = 4.5; fprintf (fptr, "in if block, x=%f\n", x); } fprintf( fptr, "after if block, x=%d\n", x); }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 7Winter Quarter Scope of Variables #include void main ( ) { int x = 3 ; /* Now "x" known, program compiles */ FILE *fptr ; fptr = fopen ("scope.dat", "w") ; fprintf (fptr, ”before if block, x=%d\n", x) ; if ( x == 3 ) { float x = 4.5 ; fprintf (fptr, "inside if block, x=%f\n", x) ; } fprintf (fptr, "after if block, x=%d\n", x) ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 8Winter Quarter Scope of Variables When the program is modified as was shown on the previous slide, x is known throughout the program, but the x inside the if block of statements has a different data type and has a different value than the one outside the block. These two versions of the variable, x, have two different memory locations, and thus we have two completely different variables with the same name. (Confusing, eh?)

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 9Winter Quarter Scope of Variables As proof, let's look at the output file from the program, which was written to "scope.dat": before if block, x=3 inside if block, x= after if block, x=3 Note that when the if block is in scope, x is a floating point variable whose value is 4.5, but outside the if block it is an integer variable with a value of 3.