Brent M. Dingle Texas A&M University Chapter 5 – Section 1

Slides:



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

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.
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.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
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 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Top Down Design Brent M. Dingle Texas A&M University Chapter 4 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
User-Defined Functions II TK1914: C++ Programming.
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
Chapter 2. Variable and Data type
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
A Simple Program CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
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.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 7: User-Defined Functions II
CS 326 Programming Languages, Concepts and Implementation
Objectives In this chapter, you will:
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
CS 326 Programming Languages, Concepts and Implementation
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Suppose we want to print out the word MISSISSIPPI in big letters.
CS 326 Programming Languages, Concepts and Implementation
JavaScript Syntax and Semantics
CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002
Anatomy of a Class & Method
Chapter 4: Writing Classes
(c)opyright Brent M. Dingle 2001
Defining Classes and Methods
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Implementing Non-Static Features
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Chapter 4 –Requirements for coding in Assembly Language
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
Classes CS 21a: Introduction to Computing I
Objectives In this chapter, you will:
Simple Branches and Loops
A simple function.
CS1100 Computational Engineering
(c)opyright Brent M. Dingle 2001
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Chapter (3) - Procedures
Complex Array Structures
Representations & Reasoning Systems (RRS) (2.2)
Methods Scope How are names handled?
Parameters and Arguments
Scope Rules.
Presentation transcript:

Brent M. Dingle Texas A&M University Chapter 5 – Section 1 Identifiers Brent M. Dingle Texas A&M University Chapter 5 – Section 1

What’s an identifier? A name used in a Pascal program is called an identifier and is defined to be any string made up of letters, digits and the underscore symbol – provided the string starts with either a letter or the underscore symbol (p. 46). Things that are identifiers: Variable names Named constants Procedure Names Some other named things that we may get to =)

Local Identifiers Recall that we saw local variables show up in procedures. Local variables are an example of local identifiers. A local variable is a variable declared within a procedure (or function). No statement outside the procedure (function) body may reference the local variable.

Other local identifiers Recall also that procedures may have local constants and local procedures declared in their declaration section. These would be other examples of local identifiers. Again local constants and local procedures may NOT be referenced outside the body of the procedure in which they were declared.

A note on local procedures Generally I would NOT recommend the usage of local procedures, however be aware that it can be done in Pascal and there may exist some scenario where it is useful.

If local exists Then global probably does too As we saw previously there also exist global variables (and global constants and global procedures). These would be examples of global identifiers. Global variables are most often what is talked about in terms of global. Variables declared for the entire program are called global variables.

Note on global variables. If at all possible, do NOT make any reference to any global variables within a procedure (or function). Procedures and functions should be self contained units that are meaningful outside the context of the program. See page 167 for more notes on this.

Formal VALUE parameters and local variables Formal value parameters are implemented as local variables. Notice: the value of formal value parameters only changes within the body of the procedure (or function). the name of the formal value parameters only can be referenced within the body of the procedure (or function). This behavior seems a lot like a local variable’s, yes?

Formal VARIABLE parameters and local variables Formal variable parameters are NOT implemented as local variables. They behave in a slightly different fashion – they allow you to alter the value of something outside the procedure.

Block definition A block is a program unit consisting of a parameter list, a set of declarations and the body of statements to which they apply.

Block example A procedure declaration consists of: the word procedure the procedure name a parameter list (that may be empty) a set of declarations (local constants, variables, and procedures) a body of statements ending with a semicolon

Block example (cont) Notice that parts 3, 4, 5 are just a block, so a procedure becomes: the word procedure the procedure name a block ending with a semicolon

Scope All identifiers described at the start of a block are said to be local to that block or rather to have that block as their scope. The meaning given to an identifier by the parameter list or by a declaration applies ONLY within the block.

Suggested Exercises (not graded) page 164, 165: 1, 2, 3, 4, 5 page 175, 176: 8, 10, 11

End Identifiers