Scope and Lifespan of Identifiers. Every function has a scope What does that mean? That means that every identifier that is created in a function (that’s.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Introduction to C Programming
A method for addressing any large problem. Carefully consider the problem. Define the problem. Ask yourself the following questions: What is it that I.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Run time vs. Compile time
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CPS120: Introduction to Computer Science Functions.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
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.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
User-Defined Functions II TK1914: C++ Programming.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A First Book of ANSI C Fourth Edition
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Advanced Programming in C
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Topic: Functions – Part 2
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
User-Defined Functions
This pointer, Dynamic memory allocation, Constructors and Destructor
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 4 void Functions
Variables and Their scope
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Starting Out with Programming Logic & Design
Scope Rules Of Variables
Introduction to Classes and Objects
Presentation transcript:

Scope and Lifespan of Identifiers

Every function has a scope What does that mean? That means that every identifier that is created in a function (that’s variables and function names) belongs to that function The identifier is accessible (useable) in that function and nowhere else, it is local to the function This is the default behavior of variables (not just in Python) It is a GOOD thing because it allows you to focus on solving the problem that the function is supposed to solve, without worrying about what other functions used for names for their variables

Global variables The other scope that exists in a program file is “global”. This is an identifier which is defined outside of ALL function definitions An identifier which is global can be accessed anywhere in the file, in any function which appears below the definition of the identifier Using Global variables is considered NOT good style – and are not allowed in this class Some people want to do it because they think it makes functions easier to write – no parameter lists! Everything is global! If parameter lists are very difficult for you, then you need to understand your function’s purpose. If you cannot decide what needs to be a parameter, ask yourself “will the function get this info from outside, or will it generate it itself?”

Why global variables are bad style If you want to copy a function to use in another program, and the function uses a global variable, you must also copy the global variable definition If you are working as part of a team on a program, if you create a global variable, there is NO WAY to prevent other team members from using/changing your global – you have no way to control access If you want a function to be general, you do NOT use global variables – it is much more difficult to keep changing the global variable between calls than it is to call the function with different arguments They make testing and debugging much harder!

Lifespan of a variable How long does a variable “live”? That is, when during the execution does a variable have memory and retain its value? This is a dynamic way of thinking about variables, as opposed to scope, which is more static If you had to describe the scope of a variable, you could print out the code of a program and circle the scope on the paper – it does not change If you had to describe the lifespan of a variable, you would have to describe when the variable was created and when it was destroyed, which varies depending on the execution of the program

Lifespans The lifespan of a variable local to a function is from the time it is created in the function’s run, until the function ends (and the memory allocated to the variable is released) It’s the same for parameters to that function also. The lifespan of a global variable is from the time its definition is encountered during the execution of the whole program, until the program is finished If a function with locals is called several times, then those variables are created and destroyed, created and destroyed, over and over for each call, their lifespan is intermittent