J. Michael MooreScope & Testing ModulesCSCE 110Influenced by material developed by James Tam & Jennifer WelchJ. Michael Moore_____________The parts of a program where an item (constant, variable, function, procedure) _______________________ for use.e.g., variables or constants must first be ________ before they can be __________________________.beginvar num: integer;num := 10;::end.J. Michael Moore_____________The parts of a program where an item (constant, variable, function, procedure) _______________________ for use.e.g., variables or constants must first be ________ before they can be __________________________.beginvar num: integer;num := 10;::end.__________________J. Michael Moore_____________The parts of a program where an item (constant, variable, function, procedure) _______________________ for use.e.g., variables or constants must first be ________ before they can be __________________________.beginvar num: integer;num := 10;: :end.Comes into ______Goes out of ___________ of _____J. Michael Moore_________ Scope_________ scope: After declaration, the item (constant, variable, function or procedure) can be accessed _____________in the program.program exampleProgram;procedure proc;varbeginend;beginend.Declarations here have __________ scopeDeclarations with ___________ scopeDeclarations with ___________ scopeJ. Michael Moore_________ ScopeWhen an identifier (constant, variable, function or procedure) is encountered the compiler will:1. Check in the _______ scope2. Check the _______ scope if no matches can be found __________For example:program exampleProgram;varnum : integer;procedure proc;varnum : integer;beginnum := 1;end;begin::end.Reference to an identifier1) Check local scope2) Check global scopeJ. Michael MooreFirst ____________ Examplescope1.pasprogram scope1 (output);constSIZE = 10;varnum1 : integer;ch : char;procedure proc1;varnum2 : real;num3 : real;beginwriteln('In proc1');end;beginend.J. Michael Moore__________________ The Use Of _________ Variables• Remember ________ variables can be ______ or _______________________ in the program after their declaration.• This results in:• ___________________ modules – changes in one module may effect other modules• Programs that are more difficult to _______and ______________.• Unless there is a _____________ reason, variables should be declared ____________ and passed as a ____________where ever it is needed.J. Michael MooreSecond Scoping Examplescope2.pasprogram scope2 (output);varnum : integer;ch : char;procedure proc1;varch : char;beginch := 'b';writeln('In proc1');writeln ('num=', num, ' ch=', ch);writeln;end;J. Michael MooreSecond Scoping Example (2)procedure proc2(numProc2: integer);varnum : integer;beginwriteln(‘In proc2’);num := 2;numProc2 := 20;writeln ('num=', num, ' ch=', ch, ' numProc2=', numProc2);writeln;proc1;end;J. Michael MooreSecond Scoping Example (3)beginvar 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 MooreGoing 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 cases2) Obviously false cases3) Boundary casesJ. Michael MooreDeveloping Modules•This is an integral part of the top down approach to designing programs.•Recall with the top down approach:1. Start with _____ level idea of solution, and ___________________________________it. (i.e., specify what modules that the program must consist of butdon’t write the code for them yet).wisdom (main)getAge calculateAgeModifierJ. Michael Moore_________________• It’s an outline of a module with the bare _____________of code needed to ___________• required ___________• module ________• a ______________ to define the body• ____________________and ______________ may or may not be included in the _________________J. Michael MooreDevelop & Test Modules2. Implement the body of each module, _________________________.# Get ageprocedure getAge (var age : integer);beginwrite('How old are you (1-113 years)? ');readln(age);end;wisdom (main)getAge calculateAgeModifierJ. Michael MooreCode ________________ For wisdom Number Generatorprogram wisdom (input, output);procedure getAge (var age : integer);beginend;function calculateWisdomNumber (age : integer): integer;begincalculateWisdomNumber := 0;end;beginvar age : integer;var wisdomNumber : integer;getAge (age);wisdomNumber := calculateWisdomNumber(age);end.J. Michael MooreImplementation Of Procedure “getAge”procedure getAge (var age : integer);beginwrite('How old are you (1-113 years)? ');readln(age);end;J. Michael MooreTesting Procedure “getAge”Testing simply involves checking the input:(* In the main procedure *)getAge(age);writeln('After getAge, age=', age);J. Michael MooreImplementing Function “calculateWisdomNumber”function calculateWisdomNumber (age : integer): integer;beginif (age >= 1) AND (age <= 30) thencalculateWisdomNumber := age * 1else if (age >= 31) AND (age <= 65) thencalculateWisdomNumber := age * 2else if (age > 65) thencalculateWisdomNumber := age * 3;elsecalculateWisdomNumber := 0;end;J. Michael MooreTesting Function “calculateWisdomNumber”(* Testing calculateWisdomNumber in the main procedure *)wisdomNumber := calculateWisdomNumber(-5);if (wisdomNumber <> 0) thenwriteln('Error if age < 1');wisdomNumber := calculateWisdomNumber(1); (* boundary *)if (wisdomNumber <> 1) thenwriteln('Error if age = 1');wisdomNumber := calculateWisdomNumber(21);if (wisdomNumber <> 21) thenwriteln('Error if age = 21');wisdomNumber := calculateWisdomNumber(30); (* boundary *)if (wisdomNumber <> 30) thenwriteln('Error if age = 30');J. Michael MooreTesting Function “calculateWisdomNumber” (2)wisdomNumber := calculateWisdomNumber(31); (* boundary *)if (wisdomNumber <> 62) thenwriteln('Error if age = 31');wisdomNumber := calculateWisdomNumber(55);if (wisdomNumber <> 110) thenwriteln('Error if age 30 - 65');wisdomNumber := calculateWisdomNumber(65); (* boundary *)if (wisdomNumber <> 130) thenwriteln('Error if age = 65');wisdomNumber := calculateWisdomNumber(90);if (wisdomNumber <> 270) thenwriteln('Error if age > 65');J. Michael MooreRecall This DesigninputAmountChange program (main)computeChangeoutputCoinsamountamountquartersdimespenniesquartersdimespenniescomputeQuarterscomputeDimescomputePenniesamountLeft
View Full Document