J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.

Slides:



Advertisements
Similar presentations
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Advertisements

James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Sub Programs To Solve a Problem, First Make It Simpler.
J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
1 Pertemuan 07 Procedures Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Making Decisions In Python
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
Functions: Decomposition And Code Reuse This section of notes shows you how to write functions that can be used to: decompose large problems, and to reduce.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Problem Decomposition This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and manage.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
Chapter 6: Functions.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Bill Tucker Austin Community College COSC 1315
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Principles of programming languages 4: Parameter passing, Scope rules
Anatomy of a Function Part 2
CS1100 Computational Engineering
Selection CSCE 121 J. Michael Moore.
6 Chapter Functions.
Group Status Project Status.
Anatomy of a Function Part 3
Reference Parameters.
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Procedures Brent M. Dingle Texas A&M University
Guidelines for Writing Functions
Breaking Problems Down
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Presentation transcript:

J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch

J. Michael Moore Scope The parts of a program where an item (constant, variable, function, procedure) is visible and available for use. e.g., variables or constants must first be declared before they can be referred to or used. begin var num: integer; num := 10;: end.

J. Michael Moore Scope The parts of a program where an item (constant, variable, function, procedure) is visible and available for use. e.g., variables or constants must first be declared before they can be referred to or used. begin var num: integer; num := 10;: end. Declaration Usage

J. Michael Moore Scope The parts of a program where an item (constant, variable, function, procedure) is visible and available for use. e.g., variables or constants must first be declared before they can be referred to or used. begin var num: integer; num := 10; : : end. Comes into scope Goes out of scope scope of num

J. Michael Moore Global Scope Global scope: After declaration, the item (constant, variable, function or procedure) can be accessed anywhere in the program. program exampleProgram; procedure proc; var begin end; begin end. Declarations here have global scope Declarations with local scope

J. Michael Moore Global Scope When an identifier (constant, variable, function or procedure) is encountered the compiler will: 1.Check in the local scope 2.Check the global scope if no matches can be found locally For example: program exampleProgram; var num : integer; procedure proc; var num : integer; begin num := 1; end; begin :: end. Reference to an identifier 1) Check local scope 2) Check global scope

J. Michael Moore First Scoping Example scope1.pas program scope1 (output); const SIZE = 10; var num1 : integer; ch : char; procedure proc1; var num2 : real; num3 : real; begin writeln('In proc1'); end; begin end.

J. Michael Moore Avoid / Minimize The Use Of Global Variables Remember global variables can be accessed or changed anywhere in the program after their declaration. This results in: Tightly coupled modules – changes in one module may effect other modules Programs that are more difficult to trace and understand. Unless there is a very good reason, variables should be declared locally and passed as a parameter where ever it is needed.

J. Michael Moore Avoid / Minimize The Use Of Global Variables Modified from:

J. Michael Moore Second Scoping Example scope2.pas program scope2 (output); var num: integer; ch: char; procedure proc1; var ch : char; begin ch := 'b'; writeln('In proc1'); writeln ('num=', num, ' ch=', ch); writeln; end;

J. Michael Moore Second Scoping Example (2) procedure proc2(numProc2: integer); var num : integer; begin writeln(‘In proc2’); num := 2; numProc2 := 20; writeln ('num=', num, ' ch=', ch, ' numProc2=', numProc2); writeln; proc1; end;

J. Michael Moore Second Scoping Example (3) begin var numLocal : integer; num := 1; ch := 'a'; numLocal := 10; writeln; proc2(numLocal); writeln('In main program'); writeln('num=', num, ' ch=', ch, ' numLocal=', numLocal); end.

J. Michael Moore Going back a bit…Testing Decision Making Constructs Make sure that the body of each decision making construct executes when it should. Test: 1)Obviously true cases 2)Obviously false cases 3)Boundary cases

J. Michael Moore Developing Modules This is an integral part of the top down approach to designing programs. Recall with the top down approach: 1.Start with high level idea of solution, and successively refine it. (i.e., specify what modules that the program must consist of but don’t write the code for them yet). wisdom (main) getAge calculateWisdomNum ber

J. Michael Moore Skeleton It’s an outline of a module with the bare minimum of code needed to compile required keywords module name a statement to define the body return values and parameters may or may not be included in the skeleton

J. Michael Moore Develop & Test Modules 2.Implement the body of each module, one-at-a-time. # Get age procedure getAge (var age : integer); begin write('How old are you (1-113 years)? '); readln(age); end; wisdom (main) getAge calculateWisdomNum ber

J. Michael Moore Code Skeleton For wisdom Number Generator program wisdom (input, output); procedure getAge (var age : integer); begin end; function calculateWisdomNumber (age : integer): integer; begin calculateWisdomNumber := 0; end; begin var age: integer; var wisdomNumber: integer; getAge (age); wisdomNumber := calculateWisdomNumber(age); end.

J. Michael Moore Implementation Of Procedure “getAge” procedure getAge (var age : integer); begin write('How old are you (1-113 years)? '); readln(age); end;

J. Michael Moore Testing Procedure “getAge” Testing simply involves checking the input: (* In the main procedure *) getAge(age); writeln('After getAge, age=', age);

J. Michael Moore Implementing Function “calculateWisdomNumber” function calculateWisdomNumber (age : integer): integer; begin if (age >= 1) AND (age <= 30) then calculateWisdomNumber := age * 1 else if (age >= 31) AND (age <= 65) then calculateWisdomNumber := age * 2 else if (age > 65) then calculateWisdomNumber := age * 3; else calculateWisdomNumber := 0; end;

J. Michael Moore Testing Function “calculateWisdomNumber” (* Testing calculateWisdomNumber in the main procedure *) wisdomNumber := calculateWisdomNumber(-5); if (wisdomNumber <> 0) then writeln('Error if age < 1'); wisdomNumber := calculateWisdomNumber(1); (* boundary *) if (wisdomNumber <> 1) then writeln('Error if age = 1'); wisdomNumber := calculateWisdomNumber(21); if (wisdomNumber <> 21) then writeln('Error if age = 21'); wisdomNumber := calculateWisdomNumber(30); (* boundary *) if (wisdomNumber <> 30) then writeln('Error if age = 30');

J. Michael Moore Testing Function “calculateWisdomNumber” (2) wisdomNumber := calculateWisdomNumber(31); (* boundary *) if (wisdomNumber <> 62) then writeln('Error if age = 31'); wisdomNumber := calculateWisdomNumber(55); if (wisdomNumber <> 110) then writeln('Error if age '); wisdomNumber := calculateWisdomNumber(65); (* boundary *) if (wisdomNumber <> 130) then writeln('Error if age = 65'); wisdomNumber := calculateWisdomNumber(90); if (wisdomNumber <> 270) then writeln('Error if age > 65');

J. Michael Moore Recall This Design inputAmount Change program (main) computeChange outputCoins amount quarters dimes pennies quarters dimes pennies computeQuarters computeDimes computePennies amountLeft dimes amountamountLeft quarters amountLeft pennies

J. Michael Moore Designing & Testing these Modules inputAmount Change program (main) computeChange outputCoins amount quarters dimes pennies quarters dimes pennies computeQuarters computeDimes computePennies amountLeft dimes amountamountLeft quarters amountLeft pennies

J. Michael Moore Designing & Testing these Modules inputAmount Change program (main) computeChange outputCoins amount quarters dimes pennies quarters dimes pennies computeQuarters computeDimes computePennies amountLeft dimes amountamountLeft quarters amountLeft pennies Alternatively