C Part 2 Computer Organization I 1 August 2009 ©2006-09 McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

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.
 Scope and linkage  Storage classes  Automatic memory  Dynamic memory  Memory Model.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
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.
Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
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 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
C++ for Engineers and Scientists Third Edition
1 Chapter 9 Scope, Lifetime, and More on Functions.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
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.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
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.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Tutorial ICS431 – Introduction to C.
18. DECLARATIONS.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
C Basics Computer Organization I 1 May 2010 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell Labs.
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.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CSCI 171 Presentation 6 Functions and Variable Scope.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
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.
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.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
The Three Attributes of an Identifier
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
The Three Attributes of an Identifier
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
C++ for Engineers and Scientists Second Edition
Scope, Parameter Passing, Storage Specifiers
Advanced Programming Basics
6 Chapter Functions.
C Storage classes.
Scope Rules and Storage Types
Scope Rules Of Variables
1-6 Midterm Review.
Predefined Functions Revisited
The Three Attributes of an Identifier
Basic Guarantees Right off the bat… what I'm going to describe is what almost certainly happens on contemporary machines, but there is absolutely nothing.
Presentation transcript:

C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential attributes: storage duration scope linkage

C Part 2 Computer Organization I 2 August 2009 © McQuain, Feng & Ribbens Storage Duration storage duration determines when memory is set aside for the variable and when that memory is released automatic allocated when the surrounding block is executed deallocated when the block terminates static stays in the same storage location as long as the program is running can retain its value indefinitely (until program terminates)

C Part 2 Computer Organization I 3 August 2009 © McQuain, Feng & Ribbens Scope scope (of an identifier) the range of program statements within which the identifier is recognized as a valid name block scope visible from its point of declaration to the end of the enclosing block place declaration within a block file scope visible from its point of declaration to the end of the enclosing file place declaration outside of all blocks (typically at beginning of file)

C Part 2 Computer Organization I 4 August 2009 © McQuain, Feng & Ribbens Linkage linkage determines the extent to which the variable can be shared by different parts of the program external linkage may be shared by several (or all) files in the program internal linkage restricted to a single file, but shared by all functions within that file no linkage restricted to a single function

C Part 2 Computer Organization I 5 August 2009 © McQuain, Feng & Ribbens Determining the Attributes When the defaults are not satisfactory, see: auto static extern register The default storage duration, scope and linkage of a variable depend on the location of its declaration: inside a block automatic storage duration, block scope, no linkage outside any block static storage duration, file scope, external linkage

C Part 2 Computer Organization I 6 August 2009 © McQuain, Feng & Ribbens Function Declaration vs Definition double circleArea(double Radius) { const double PI = ; return (PI * Radius * Radius); } Function declarations are typically declared at file scope or within a header file, although they may be placed anywhere. The placement determines the scope of the function name, and hence where it may be called. double circleArea(double Radius); The function declaration is essentially just a copy of the function header from the function definition. A function declaration must specify the types of the formal parameters, but formal parameter names are optional. However, it is good practice to include the formal parameter names in the function declaration. Many authors refer to a function declaration as a prototype. A function name is an identifier, so it must be formally declared before it is used. Unlike a simple variable or constant, a function has a return type and (possibly) a formal parameter list. The function declaration must also specify those.

C Part 2 Computer Organization I 7 August 2009 © McQuain, Feng & Ribbens Formal Parameters are Pass-by-Value Formal parameters have automatic storage duration and block scope, just like local variables. Formal parameters are automatically initialized with a copy of the value of the corresponding actual parameter. There is no connection between the actual and formal parameters other than that they store the same value at the time of the function call. In Java, you can pass a function a reference to an object (in fact, there's no other way to pass an object) and the function can use the reference to modify the object that's held by the calling code. In C, you can't do that until you understand how pointers work…

C Part 2 Computer Organization I 8 August 2009 © McQuain, Feng & Ribbens Typical C Code Organization // include directives... // file-scoped declarations of functions // and constants... int main() {... } // implementations of other functions For now, we'll restrict our attention to single-file programs. The typical organization is:

C Part 2 Computer Organization I 9 August 2009 © McQuain, Feng & Ribbens Example Program #include int nextPrimeAfter(int Start); bool isPrime(int Value); int main() { int Value; printf("Enter a positive integer: "); fflush(stdout); scanf("%d", &Value); printf("\n"); while ( Value <= 0 ) { printf("No, I said a POSITIVE integer: "); scanf("%d", &Value); printf("\n"); } // continues...

C Part 2 Computer Organization I 10 August 2009 © McQuain, Feng & Ribbens Example Program //... continued int Prime = 2; while ( Value > 1 ) { bool factorFound = false; while ( Value % Prime == 0 ) { if ( !factorFound ) printf("Prime factor: "); printf("%5d", Prime); factorFound = true; Value = Value / Prime; } if ( factorFound ) printf("\n"); Prime = nextPrimeAfter( Prime ); } return 0; }

C Part 2 Computer Organization I 11 August 2009 © McQuain, Feng & Ribbens Example Program int nextPrimeAfter(int Start) { int Value = Start + 1; while ( !isPrime( Value ) ) { ++Value; } return Value; }

C Part 2 Computer Organization I 12 August 2009 © McQuain, Feng & Ribbens Example Program bool isPrime(int Value) { int Ceiling = (int) sqrt( Value ); for (int Divisor = 2; Divisor <= Ceiling; Divisor++) { if ( Value % Divisor == 0 ) return false; } return true; }