110 likes | 180 Views
ITEC 320. Lecture 7 Scope Exceptions. Review. Functions Procedures. Outline. Scope Exceptions. No-parameters. procedure demo is procedure printlines is begin put ("------------------"); put ("------------------") ; end printlines ; begin printlines ; -- do something
E N D
ITEC 320 Lecture 7 Scope Exceptions
Review • Functions • Procedures
Outline • Scope • Exceptions
No-parameters procedure demo is procedure printlines is begin put("------------------"); put("------------------"); end printlines; begin printlines; -- do something printlines; end demo;
Global procedure main is MV: constant Natural := 10; subtype MyRange is Integer range 1 .. MV; m1, m2: MyRange; -- procedure p(m: MyRange) is someLocal: Natural; begin ... m1 := 2 * m2; ... end p; procedure q is begin p(3); end q; begin -- main q; end main;
Exceptions • What are they? • Common ones • Data input errors • End of file errors • Constraint errors
Examples procedure demo_exceptions is i: natural; begin get(i); -- do something with i exception when data_error => put_line("found non-numeric input "); when end_error => put_line("end of file reached"); when constraint_error => put_line("integer not a natural"); when others => put_line("integer not a natural"); end demo_exceptions;
Places • Handle everything • Handle specific parts (Add in a begin / end) • Others • Create your own VN:Exception and raise VN • Web example
Problems • Given n of 1 or more, return the factorial of n, which is n * (n-1) * (n-2) ... 1. Compute the result recursively (without loops).
Problem • Given a string, compute recursively (no loops) the number of times lowercase "hi" appears in the string.
Summary • Scope • Exceptions