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.

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.
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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Access to Names Namespaces, Scopes, Access privileges.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Scope.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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).
1 Chapter 9 Scope, Lifetime, and More on Functions.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
Variable Scope Storage Class Recursion
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
1 Chapter 9 Scope, Lifetime, and More on Functions.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Copyright © Curt Hill Flow of Control A Quick Overview.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
A First Book of ANSI C Fourth Edition
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Advanced Programming in C
Lesson #5 Repetition and Loops.
Department of Computer Science
Lesson #5 Repetition and Loops.
Storage class in C Topics Automatic variables External variables
Chapter 5 Conclusion CIS 61.
Programming Fundamentals Lecture #7 Functions
Global & Local Identifiers
Conditinoal Constructs Review
Chapter 9 Scope, Lifetime, and More on Functions
Functions BIS1523 – Lecture 17.
Lesson #5 Repetition and Loops.
Scope, Visibility, and Lifetime
6 Chapter Functions.
Conditinoal Constructs Review
Compound Statements A Quick Overview
Local Variables, Global Variables and Variable Scope
Chapter 6: Repetition Statements
Writing Functions.
Namespaces How Shall I Name Thee?.
The lifespan of a variable
Namespaces, Scopes, Access privileges
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Function.
Scope J. Michael Moore.
Lesson #5 Repetition and Loops.
CS1201: Programming Language 2
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
STORAGE CLASS.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
STORAGE CLASS.
Function.
The Three Attributes of an Identifier
Methods Scope How are names handled?
Scope Rules.
Presentation transcript:

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 them, so the scope of a variable starts when it is declared  If the variable is declared within a block (compound statement, { } ) it only stays alive until the end of the block If the block is the one surrounding the entire function body, the variable is alive from where it is declared until the end of the function If the block defines a loop body or if-statement body, the variable only lives till the end of loop/if You can add a block anywhere you want in the code, and it will define the scope for any variables declared within it

Variable Scope Example scopes int main ( ) { int i; for (i=0; i < 10; i++ ) { int total = i; } int j = total; // error! total out of scope { int k; // use k } int m = j; … } i j m total k

Variable Scope In C++, you can reuse names, as long as they are not in overlapping scopes  In fact, you can reuse names in a scope which is nested inside another scope int main ( ) { int i = 5, j = 0; for (j = 0; j < 10; j++) { int i = j;// OK, this is new i int k = 5; doSomething (i); } int sum = k; // compile error, no k in scope j = i; // sets j to 5 for (j = 0; j < 100; j++ ) { int i = j; // yet another new i } int i = 0; // compile error –redefined variable }

Variable Scope All local scope defined by blocks There is another kind of scope, called global scope  This is for variables defined outside of functions  Global variables have scope from the point they are defined throughout the rest of file  Local variables of same name can be nested inside global variables int total = 5; int main ( ) { int total = 4; // OK, this is nested scope …. } int sub1 ( ) { int i = total; // OK, i set to 5 }

In Class Exercise int i = 10; int main ( ) { i ? for (j = 0; j < 10; j++ ) { int i = 20; i? } i ? int i = 30; i ? } More example

Variable Scope Style rules  Try to minimize scope Only use global variables if you really, really have to !!!  2 different approaches for local variables inside a function 1.Declare all variables at the top of the function This is the way you used to have to do it in C Helps the reader to know where to look for the variable declaration 2.Declare variables as they are needed Minimizes scope Allows you to set the value only once, rather then once at declaration and then again at first use  Either approach is OK – probably the most common in industry is to declare as needed  Don’t re-use names heavily, except for maybe i, j, k