Introduction to Computers and Programming Lecture 10 Recap → relational operators are defined number Prof. I. K. LundqvistReading: FK pp. 175-182, 193-194, 347-353 Sept 24 2003• User defined types –Scalar types • Ordered • Each value of a discrete type has a position – Operations on scalar types –Sub-types –Enumeration types – Derived types •Packages –Procedures – Functions – Formal vs. actual parametersScope of Declarations point? subprogram that contains it Visibility procedure P is X : Integer;procedure Q is beginX := 2;P.X :=3end Q;beginQ;end; A declaration can be hidden from direct visibility, but not hidden from all visibility, and can be accessed using selector syntax: • Where does a given declaration apply? • What declarations apply at a given • Scope of a declaration – From where it is made, to the end of the X : Integer; -- hides outer declaration -- local decl. directly visible ; -- global decl. Visible, -- but not directlyVisibility type T is (A, B, C, D); procedure P (X : T ); type T1 T; procedure P (X : T1 ); once an inherited declaration is overridden, there is no way to name it: Example 1 with TEXT_IO; use TEXT_IO;2 3 procedure main is 4 5 length : constant := 4; 7 num : INTEGER;8 9 10 procedure in INTEGER) is 11 13 14 begin 16 end one;17 18 is new -- inherited P is visible -- inherited P is hidden from all visibility Some declarations are hidden from all visibility, in particular 6 str : string (1..length); one (num, len : 12 str : string (1..10); -- one 15 ... ... ... ...19 X, Y : FLOAT;20 21 22 procedure in INTEGER) is 23 24 X : INTEGER;25 26 begin 28 end two;29 30 31 begin 33 end main; two (len : -- two 27 ... ... ... ... -- main 32 ... ... ... ... Visibility of each name Name Line one two main PUT, etc 1 length 5 str 6 num 7 num 10 len 10 str 12 X 19 Y 19 len 22 X 24Visibility of each name Name Line one two main PUT, etc 1 Y Y Y length 5 Y Y Y str 6 N Y Y num 7 N Y Y num 10 Y N N len 10 Y N N str 12 Y N N X 19 N N Y Y 19 N Y Y len 22 N Y N X 24 N Y N Packages needs • Collection of resources • Encapsulated in one unit • Single library unit – Free-standing unit • Must contain its own declarations for everything it – Compiled on its own • Incorporated in other programs via ‘with’ • Compilation order: – Library unit – Procedures that use itPackage Organization • specification • body • uses the package Package Specification • package package_name is declarations privatetype definitions end package_name; public portion private portion Package show “what” it provides Package defines “how” it is implemented Both are separate from the user’s program that •Public: – What you need to know to use the package • Private: – Implementation of data types Courtesy of Chris Lokan. Used with permission.Private Typespackage accounts is type account is privateprocedure account; amount : in money); procedure deposit(an_account: account; amount : in money); function return account; function return integer; private type account is record account_no :positive;balance :integer;;end accounts; Package Body provided by the package is what the package provides. user of the package. package user. ; -- declaration comes later withdraw(an_account : in out in out create(initial_balance : money) balance( an_account : account) -- this part of the package specification -- contains the full description. end record• Implementation of the resources • All a user of the package needs to know • The package is a "black box" to the • The package body is not visible to aPackage Body package_name is declarations end package_name; Package Example specification .ads package PLANIMETRY is type length 5 range 0.0 .. 1.0E10;type area 5 range 0.0 .. 1.0E20; function area_rectangle (L,H : length) return area;function area_circle (R : length) return area;function area_triangle (B,H : length) return area;function circumf_circle (R : length) return length; end PLANIMETRY; package body is digits is digitsPackage Example body .adb PLANIMETRY is PI : constant := 3.1415926536; function area_rectangle (L,H : length) return area is beginreturn area(L) * area(H);end; function area_circle (R : length) return area is beginreturn PI * area(R) ** 2;end; Package Example body .adb function area_triangle (B,H : length) return area is beginreturn area(B) * area(H) / 2.0;end; function circumf_circle (R : length) return length is beginreturn 2.0 * PI * R;end;end PLANIMETRY; package bodyUsing Packages – Ada.Text_Io.new_line;int_io.put (mark, width => 1);planimetery.area_circle (2.0); – use Ada.Text_io, int_io, planimetry; … put (“abc”);new_line;put (mark, width => 1);area_circle (2.0); User Program with TEXT_IO, PLANIMETRY; procedure main is use TEXT_IO;... declarations L : PLANIMETRY.length;H : PLANIMETRY.length;A : PLANIMETRY.area;R : PLANIMETRY.length; beginR := ... ;A := PLANIMETRY.area_circle (R);end main; • To use a package element – package.element •Example Ada.Text_Io.put (item => “abc”); • USE allows package to be omitted -- length -- height -- area -- radiusCase Statement • – – selector statement_before; case selector is when value_list_1 => statement(s)_1;when value_list_2 => statement(s)_2; … when others => statement(s)_n;; statement_after; Selectors discrete value Used for multiple selections Alternative to multiple if Used when we can explicitly list all alternatives for one end case• Variable or expression resulting in a • Selector value_list may be: – A single constant value, e.g., ‘a’ – A series of alternatives, e.g., ‘a’ | ‘b’ | ‘c’ – A range of values e.g., ‘a’ .. ‘z’ – Or any combination of the aboveRestrictions on Case Statements • once in a case statement • must be supplied, either explicitly or using when others. • when others indicates the action when none of the listed when alternatives are matched – – null;" statement Case vs Multiple if values A particular value may only occur All possible values of the selector it must be the last alternative to specify no action, use the "•Case – Table of values and actions – Easy to operate specify range of selector – Easy to specify alternative selector values •Multiple if –
View Full Document