Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Advertisements

EC-241 Object-Oriented Programming
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.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : … Suppose we need.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 Chapter 9 Scope, Lifetime, and More on Functions.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Object Oriented Programming Spring COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSE202: Lecture 13The Ohio State University1 Function Scope.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Functions Scope local global Global Resolution Operator part 5.
Function Overloading Can enables several function Of same name
A Lecture for the c++ Course
Storage class in C Topics Automatic variables External variables
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Global & Local Identifiers
Function Basics.
Chapter 9 Scope, Lifetime, and More on Functions
Chapter 15 Pointers, Dynamic Data, and Reference Types
User Defined Functions
Chapter 5 Function Basics
Pointers, Dynamic Data, and Reference Types
6 Chapter Functions.
Counting Loops.
Chapter 15 Pointers, Dynamic Data, and Reference Types
The Run-Time Stack and Reference Parameters
Static in Classes CSCE 121 J. Michael Moore.
Object Oriented Programming Using C++
CS150 Introduction to Computer Science 1
Introduction to Programming
Default Arguments.
Function Overloading.
Dynamic Memory.
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Static Variables

Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.  Static variables remain and retain their value. void f( ) { static etc }

Rules for Static Variables 1.A static variable declaration is only executed once, the first time the function is executed. 2.A static variable is initialized only once (since this is part of the declaration process) and will be initialized to 0 unless the programmer designates otherwise. 3.Subsequent invocations of the function in which a static variable resides will retain the last value of that variable.

void my_function ( ) { static short count = 1; cout << "this function has been called " << count << (count == 1 ? "time" : "times"); count++; return; } int main() { for (int i = 0; i < 5; i++) my_function(); return 0; }

void is_it_larger (const float val) { static float largest = val; if (largest < val) // new val is larger largest = val; cout << "The largest value sent to this function " << “so far is " << largest << endl; return; } int main() { for (int i = 0; i < 7; i++) is_it_larger(i); return 0; }

#include // access to time clock long my_rand() { static long seed = time(NULL); seed = ( seed * 7919) % ; return abs(seed); } int main() { for (int i = 5; i > 0; i--) cout<<my_rand()<<endl; return 0; }

End of Session