Functions Scope local global Global Resolution Operator part 5.

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.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Computer Science 1620 Function Scope & Global Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
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.
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.
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 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Storage Classes.
Chapter 6: Functions.
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).
C++ for Engineers and Scientists Third Edition
Functions g g Data Flow g Scope local global part 4 part 4.
Functions Structured Programming 256 Chapter 6 Functions g prototypes g arguments g overloading g return values part I.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
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.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Variable Scope Storage Class Recursion
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 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.
Functions g g Data Flow g Scope local global part II g Global Resolution Operator part II.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
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.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 6 Modularity Using Functions
Functions Course conducted by: Md.Raihan ul Masood
IS Program Design and Software Tools Introduction to C++ Programming
C Functions -Continue…-.
A Lecture for the c++ Course
Functions Scope local global Global Resolution Operator part II
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-defined Functions
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Arrays Kingdom of Saudi Arabia
6 Chapter Functions.
User-defined Functions
CHAPTER 6 GENERAL-PURPOSE METHODS
Scope Rules and Storage Types
Variables have attributes
Based on slides created by Bjarne Stroustrup & Tony Gaddis
STORAGE CLASS.
Presentation transcript:

Functions Scope local global Global Resolution Operator part 5

int x = 3; // global because before main void main(void) { // no variables local to main( ) void myfunction( ); // prototype cout <<"x = "<<x<<" before the function call.\n"; myfunction( ); cout <<"x = "<<x<<" after the function call.\n"; } void myfunction( ) { int r; // local to myfunction( ) r = x++; cout <<"r = "<<r<<" within the function.\n"; // what happens to global x?

Scope OUTPUT x = 3 before the function call. r = 3 within the function. x = 4 after the function call.

What will be the output? Do x or y change? void swap(int, int); // a global function void main(void) { int x = 5, y = 10; 1. cout <<“Main-before swap, x: “<<x<<" y: "<<y<< '\n'; swap(x, y); 2. cout <<"Main-after swap, x: "<<x<<" y: "<<y<<'\n'; } void swap(int x, int y) { int temp; 3. cout <<"Swap-before swap, x: "<<x<<" y: "<<y<<'\n'; temp = x; x = y; y = temp; 4. cout <<"Swap-after swap, x: "<<x<<" y: "<<y<<'\n'; What will be the output? Do x or y change?

Scope OUTPUT 1. Main-before swap: x: 5 y: 10 3. Swap-before swap: x: 5 y: 10 4. Swap-after swap: x: 10 y: 5 2. Main-after swap: x: 5 y: 10

Scope within a block - Ex. 1 predict the output void scope2(void); // function prototype void main(void) { int v=100; cout <<"v BEFORE function = "<<v<<'\n'; scope2(); cout <<"v AFTER function = "<<v<<'\n'; } slide 1 of 2

Scope within a block - Ex. 1 predict the output 1. void scope2(void) //function header 2. { double v = 5.5; 3. int k, j; 4. cout << "v outside block = " << v<<'\n'; 5. for (k=1; k<=3; k++) 6. { int v = 17; // initialized in function 7. for (j=1; j<=2; j++) 8. { v++; 9. cout << "v inside block = " << v<<'\n'; 10. } 11. } 12. cout << "v outside block = " << v<<'\n'; 13. } slide 2 of 2

} } Scope within a block j loops k loop * OUTPUT 100 BEFORE 5.5 outside 18 inside 19 inside 5.5 outside 100 AFTER } k loop j loops } *

Variable Storage Classes - Section 6.5 Local auto static register ý while (l l l) { int k = 1; k++; l l l } What we’ve seen so far Sticks around between calls register - same as auto except memory in high speed registers while (l l l) { static int k = 1; k++; l l l } * * *

Variable Storage Classes Global static extern ý Hidden from other files an extern variable’s scope is extended into a separate file See Section 6.5 and attempt Exercise 6.6 on page 286

Scope & Storage Classes - an example int x = 1; // global variable main() { int x = 5; // local variable to main cout << "local x in outer scope of main is " << x << endl; { // start new scope int x = 7; cout << "local x in inner scope of main is " <<x<< endl; } // end new scope from Deitel & Deitel 1e, fig 3.12

Scope & Storage Classes - an example (continued) a(); // a has automatic local x b(); // b has static local x c(); // c uses global x a(); // a reinitializes automatic local x b(); // static local x retains its previous value c(); // global x also retains its value cout << "local x in main is " << x << endl; return 0; } // end of main() from Deitel & Deitel 1e, fig 3.12

Scope & Storage Classes - an example void a(void) { // automatic x int x = 25; // initialized each time a is called cout << endl << "local x in a is " << x << " after entering a" << endl; ++x; cout << "local x in a is " << x << " before exiting a" << endl; } from Deitel & Deitel 1e, fig 3.12

Scope & Storage Classes - an example void b(void) { static int x = 50; // Static initialization only // first time b is called cout << endl << "local static x is " << x << " on entering b" << endl; ++x; cout << "local static x is " << x << " on exiting b" << endl; } from Deitel & Deitel 1e, fig 3.12

Scope & Storage Classes - an example void c(void) { // uses x from nearest enclosing scope (global) cout << endl << "global x is " << x << " on entering c" << endl; x *= 10; cout << "global x is " << x << " on exiting c" << endl; } from Deitel & Deitel 1e, fig 3.12

Scope & Storage Classes - an example b(); c(); a(); // a has automatic local x b(); // b has static local x c(); // c uses global x a(); // a reinitializes automatic local x b(); // static local x retains its previous value c(); // global x also retains its value from Deitel & Deitel 1e, fig 3.12 *

Scope & Storage Classes - an example OUTPUT local x in outer scope of main is 5 local x in inner scope of main is 7 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c from Deitel & Deitel 1e, fig 3.12

Scope & Storage Classes - an example local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5 from Deitel & Deitel 1e, fig 3.12 What would the output be if each of the functions were called again?

Global (Scope) Resolution Operator :: double bignum = 999.99; // global void main(void) { double bignum = 120.3; // local cout<< bignum << “ = bignum, local\n” cout<< ::bignum << “ = bignum, global\n”; } OUTPUT 120.3 = bignum, local 999.99 = bignum, global * *

The Scope Resolution Operator :: void a(void) { int x = 25; // initialized each time a is called cout << endl << "local x in a is " << x << " after entering a" << endl; ++x; cout << "local x in a is " << x << " before exiting a" << endl; } from Deitel & Deitel 1e, fig 3.12 << ‘\t’ << ::x << endl; What will print here? *

Misuse of Globals Globals allow programmers to “jump around” the safeguards provided by functions Do not make all variables global! Disastrous in larger programs with many user-defined functions Sometimes used in large programs to define a few variables and constants that must be shared by many functions Small programs almost never need globals

Common Errors Passing incorrect data types Using the same variable name for different variables ex. local - in both the calling and called functions global - must use ::

Common Errors Wrong positioning of the called function prototype Terminating a function header with a ; Forgetting the data type of a function’s parameter Remember the NOT rule: Number Order Type

Debugging Prevention - plan first! Valuation tables Display values

“Heavier-than-air flying machines are impossible.” Lord Kelvin, End Note 1 “Heavier-than-air flying machines are impossible.” Lord Kelvin, President of the Royal Society, 1895