The Three Attributes of an Identifier

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.
 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.
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.
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.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
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.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
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 Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
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.
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.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
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
Introduction to Programming
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
C++.
Functions and Structured Programming
A Lecture for the c++ Course
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Instructor: Ioannis A. Vetsikas
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
6 Chapter Functions.
Classes and Objects.
C Storage classes.
Scope Rules and Storage Types
Chapter 7: User-Defined Functions II
Scope Rules Of Variables
Based on slides created by Bjarne Stroustrup & Tony Gaddis
1-6 Midterm Review.
Predefined Functions Revisited
The Three Attributes of an Identifier
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.
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:

The Three Attributes of an Identifier Identifiers have three essential attributes: storage duration scope linkage CS@VT August 2009 ©2006-09 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) CS@VT August 2009 ©2006-09 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) CS@VT August 2009 ©2006-09 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 CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens

Determining the Attributes 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 When the defaults are not satisfactory, see: auto static extern register CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens

Function Declaration vs Definition 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. 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. double circleArea(double Radius); double circleArea(double Radius) { const double PI = 3.141596224; return (PI * Radius * Radius); } Many authors refer to a function declaration as a prototype. 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. CS@VT August 2009 ©2006-09 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… CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens

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

Example Program #include <stdio.h> #include <math.h> #include <stdbool.h> 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: "); } // continues. . . CS@VT August 2009 ©2006-09 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; CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens

Example Program int nextPrimeAfter(int Start) { int Value = Start + 1; while ( !isPrime( Value ) ) { ++Value; } return Value; CS@VT August 2009 ©2006-09 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; CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens