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.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Chapter 5 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Function Scope & Global Variables.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
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.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
1 Chapter 9 Scope, Lifetime, and More on Functions.
Functions g g Data Flow g Scope local global part 4 part 4.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
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.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
1 Chapter 9 Scope, Lifetime, and More on Functions.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Chapter 2. Variable and Data type
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CHAPTER 8 Scope, Lifetime, and More on Functions.
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.
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.
chapter 8 Scope,Lifetime,and More on Functions
ㅎㅎ Fourth step for Learning C++ Programming Namespace Function
Global & Local Identifiers
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
Static Data Member and Functions
Lecture 4-7 Classes and Objects
Chapter 9 Scope, Lifetime, and More on Functions
User Defined Functions
Anatomy of a Function Part 1
Local Variables, Global Variables and Variable Scope
Introduction to Programming
Namespaces How Shall I Name Thee?.
Local, Global Variables, and Scope
Predefined Functions Revisited
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.
Anatomy of a Function Part 1
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Programming Fundamental
Scope Rules.
Presentation transcript:

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 types of Scope –Class Scope –Local Scope –Global Scope

Class Scope This term refers to the data type called a class. We will not talk about classes until 1704.

Local Scope The scope of an identifier declared inside a block extends from the point of declaration to the end of that block. Also, function parameters (formal parameter) extends from the point of declaration to the end of the block that is the body of the function.

Global Scope An identifier declared outside of all functions and classes extends from the point of declaration to the end of the entire file containing the program code. Global scope should be avoided at all cost at this point in your programming career.

Example int gamma;//global scope int main() { gamma = 3; … } void someFunc() { gamma = 5; … }

Name Precedence Identifiers, inside of a nested block with the same name as an identifier in an enclosing block, hide the identifier in the enclosing block. Also called name hiding.

#include using namespace std; void someFunc( float ); const int a = 17; //global const int b; //global variable int c; //global variable int main() { b =4; c = 6; someFunc ( 42.8 ); return 0; }

void someFunc( float c ) { float b; //prevents access to //global b b = 2.3; cout << “a = “ << a; // 17 cout << “ b = “ << b; // 2.3 cout << “ c = “ << c; // 42.8 }

Scope Rules!! Non-local identifier: With respect to a given block, any identifier declared outside that block 1.A function name has global scope. Function definitions cannot be nested within function definitions. 2.The scope of a function parameter is identical to the scope of a local variable declared in the outermost block of the function body.

More Scope Rules!! 3.The scope of a global variable or constant extends from its declaration to the end of the file. 4.The scope of a local variable or constant extends from its declaration to the end of the block in which it is declared. This scope includes any nested blocks 5.The scope of an identifier does not include any nested blocks that contain a locally declared identifier with the same name (local identifiers have name precedence). Rules taken from page 375 of your book.

void block1 ( int, char& ); void block2 (); int a1; // global int a2; // global int main() { … }

void block1 ( int a1, //prevents //access to global a1 char& b2 ) { int c1;//has same scope as int d2;// b2,c1,d2; … }

void block2() { int a1; //blocks global a1 int b2; //local; no conflict while (…) {//block3 int c1; //local to block3 int b2; //blocks non-local b2 … } }

Namespaces What is a namespace? –Basically a named scope. –This allows you to hide identifiers inside of a scope to avoid name clashes. –You must use one of the three namespace resolution methods to access the identifiers inside of a namespace

Example In cstdlib namespace std { … int abs( int ); … }

Fourth Scope Namespace Scope –The scope of an identifier declared in a namespace definition extends from the point of declaration to the end of the namespace body and its scope includes the scope of a using directive specifying that namespace

Lifetime of a Variable Lifetime –The period of time during program execution when an identifier has memory allocated to it Automatic Variable –A variable for which memory is allocated and de- allocated when control enters and exits the block in which it is declared Static Variable –A variable for which memory remains allocated throughout the execution of the entire program

Example void someFunc( int someParam ) { int i = 0;//initialized each time int n = 2;//initialized each time static char ch = ‘A’;//Initialized once … }

Interface Design Side Effect –Any effect of one function on another that is not part of the explicitly defined interface between them.

void CountInts(); int count; int intVal; int main() { count = 0; cin >> intVal; while ( cin ){ count++; CountInts(); cin >> intVal; } cout << count << “ lines of input processed.\n”; return 0; }

void CountInts() { count = 0; while ( intVal != ) { count++; cin >> intVal; } cout << count << “ integers on this line.\n”; }

Value-Returning Functions Function Value type –the data type of the result value returned by a function Example int Day ( /* in */ int month, /* in */ int dayOfMonth, /* in */ int year ) Not limited to only numeric returns, can return any data type

Rules of Thumb 1.If the module must return more than one value or modify any of the caller’s arguments, do not use a value-returning function 2.If the module must perform I/O, do not use a value return function. 3.If there is only one value returned and it is a Boolean value, use a value returning function.

More Rules of Thumb 4.If there is only one value returned and that value is to be used immediately in an expression, use a value returning function. 5.When in doubt, use a void function. 6.If both a void function and a value- returning function are acceptable, use the one you feel more comfortable with. From page 399 in your book.