Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.

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.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, 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.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Using Abstraction to Manage Complexity Abstraction: procedural abstraction & data abstraction. Procedural abstraction=> function development should separate.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
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 Chapter 9 Scope, Lifetime, and More on Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
chap13 Chapter 13 Programming in the Large.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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 6: Functions.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
CSCI 171 Presentation 6 Functions and Variable Scope.
Week 11 Multi-file Programs and Scope of Variables.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
#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.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CHAPTER 8 Scope, Lifetime, and More on Functions.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Advanced Programming in C
chapter 8 Scope,Lifetime,and More on Functions
Chapter 7: User-Defined Functions II
A Lecture for the c++ Course
Storage class in C Topics Automatic variables External variables
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
6 Functions.
User-defined Functions
Chapter 9 Scope, Lifetime, and More on Functions
User-defined Functions
Scope Rules and Storage Types
Chapter 7: User-Defined Functions II
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.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Presentation transcript:

Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and lasts until the end, it is taking up lots of memory –difficult to debug

Methods of variable creation (continued) Local on the fly variables –simply created when they are needed, –only available from within the routine in which they were created –memory requirement is less –easy to debug

Methods of variable creation (continued) Local defined variable –created before they are needed –only available in the routine in which they were created. –Memory requirement is less –easy to debug –the most usually favored method

Scope The scope of an identifier is the portion of the program in which the identifier can be referenced. Some identifiers can be referenced throughout the program. Others can be referenced from only portions of a program. Example: When we declare a local variable in a block, it can be referenced only in that block or in blocks nested within that block.

A program written in C++ 1 // This program calculates the number calories in a cheese sandwich 2 #include 3 const int BREAD = 63; // global constant 4 const int CHEESE = 106; // global constant 5 const int MAYONNAISE = 49; // global constant 6 const int PICKLES = 25; // global constant 7 int main() 8 { 9 int totalCalories; // local variable 10 totalCalories = 2 * BREAD + CHEESE + MAYONNAISE + PICKLES; 11 cout << "There were " << totalCalories; 12 cout << " calories in my lunch yesterday."<< endl; 13 return 0; 14 }

Function definition and function prototype // A programmer-defined square function #include int square (int); // Function prototype int main() { int x; //local variable for (x = 1; x <= 10; x++) cout << square(x) << ‘ ‘; cout << endl; return 0; } int square (int y) // Function definition { return y * y; } Local variable

Storage duration Period during which an identifier exists in memory. Some identifiers exists briefly. Some are repeatedly created and destroyed. Others exist for the entire execution of a program. Storage duration can be of two types: –automatic (auto and register) –static

Global and Local declarations #include void func( float ); const int a = 17; // global constant int b; // global variable int c; // global variable int main() { b = 4; // assignment to global b c = 6; // assignment to global c func(42.8); return 0; } void func( float c) // prevents access to global c { float b; // prevent access to global b b = 2.3; // assignment to local b cout << “ a = “ << a; // output global a (17) cout << “ b = “ << b; // output local b (2.3 cout << “ c = “ << c; // output local c (42.8) } Output: A = 17 b = 2.3 c = 42.8

Explanation In this example, function func accesses global constant a. However, func declares its own local variable b and parameter c. Local variable b takes precedence over global variable b, effectively hiding global variable b from the statements in function func. Function parameter acts like local variable.

Life time of a variable Local variables: variables declared inside a function or variables declared as function parameter. When you will call the function, memory will be allocated for all local variables defined inside the function. Finally memory will be deallocated when the function exits. The period of time a variable will be “alive” while the function is executing is called “lifetime” of this variable.

Storage Classes C++ provides four storage classes: –auto –register –extern –static An identifier’s storage class helps determine its storage duration and scope.

Auto Storage class Formal parameters and local variables of functions are variables that are automatically allocated on the stack when a function is called and automatically deallocated when the function returns. They are of storage class auto.

Extern Storage class of names known to the linker. Example: extern int square (int x); Means the function will be available to the linker. It notifies the compiler that such a function exists and that the linker will know where to find it.

Register If you declare a variable of type register, it simply alerts the compiler to the fact that this memory cell will be referenced more often than most. Register is a special high-speed memory location inside the central processor.

Static Static variable is allocated and initialized one time, prior to program execution. It remains allocated until the entire program terminates.

Namespaces (page 400) Namespace means scope. In C++, namespace is a mechanism by which a programmer can create a named scope. For example, the standard header file cstdlib contains function prototypes for several library functions, one of which is the absolute value function, abs. namespace std {.. int abs (int );.. }

Namespace Identifiers declared inside the namespace body have namespace scope. Identifiers defined inside the body can be accessed by three ways. First method: #include int main() { int alpha; int beta;.. alpha = std::abs(beta); } Scope resolution operator

Namespace Second Method: #include int main() { int alpha; int beta; using std::abs; … alpha = abs(beta); … }

Namespace Third Method: #include int main() { int alpha; int beta; using namespace std; … alpha = abs(beta); … }