Download presentation
Presentation is loading. Please wait.
Published byJonas Curtis Modified over 9 years ago
1
ITEC 320 Lecture 5 Scope Exceptions
2
Scope / Exceptions Review Functions Procedures
3
Scope / Exceptions Outline Scope Exceptions Variable sized arrays
4
Scope / Exceptions 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).
5
Scope / Exceptions Problem Given a string, compute recursively (no loops) the number of times lowercase "hi" appears in the string.
6
Scope / Exceptions No- parameters procedure demo is procedure printlines is begin put("------------------"); end printlines; begin printlines; -- do something printlines; end demo;
7
Scope / Exceptions 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;
8
Scope / Exceptions Exceptions What are they? –Common ones Data input errors End of file errors Constraint errors
9
Scope / Exceptions 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;
10
Scope / Exceptions Places Handle everything Handle specific parts (Add in a begin / end) Others –Create your own VN:Exception and raise VN –Web example
11
Scope / Exceptions Questions What is the problem with ADA arrays? Why is this problem important? What are the downsides to the current method of using arrays?
12
Scope / Exceptions Solution Declaration procedure declare_example is size: natural; begin get(size); declare a: array(1.. size) of Integer; begin for i in a'range loop get(a(i)); end loop; end; end declare_example;
13
Scope / Exceptions More with ada.text_io; with ada.integer_text_io; use ada.text_io; use ada.integer_text_io; procedure declare_example2 is type My_Array_Type is array(Natural range <>) of Integer; size: natural; begin get(size); declare a: My_Array_Type(1.. size); begin for i in a'range loop get(a(i)); end loop; for i in a'range loop put(i); end loop; end; end declare_example2;
14
Scope / Exceptions Parameter s with ada.text_io; with ada.integer_text_io; use ada.text_io; use ada.integer_text_io; procedure declare_example3 is type My_Array_Type is array(Natural range <>) of Integer; procedure putArray(a: My_Array_Type) is begin for i in a'range loop put(i); end loop; end putArray; a: My_Array_Type(1.. 10); begin for i in a'range loop get(a(i)); end loop; putArray(a); end declare_example3;
15
Scope / Exceptions Issue Named type required for passing to functions / procedures procedure demo is values1: array (1.. 50) of integer; values2: array (1.. 50) of integer; begin values1 := (2 => 4, 4 => 16, 5 => 25, 3 => 9, others => 0); - values2 := (others => 0); end demo; What is values 1? Can’t duplicate when passing to a function.
16
Scope / Exceptions Summary Scope Exceptions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.