Whirlwind Tour of Ada - Part 3b
Arrays, Procedures
Parameter Modes, Numeric Input, Exceptions
Arrays and Array Types (countposzeroneg.adb)
- Demonstrates Arrays and Array Types:
countposzeroneg.adb and prettified
- Things to note:
- The program defines a new type for the array
-
Int_Array
is a type; theNums
is an array
variable
- Arrays can be assigned using an aggregate assignment (ie the list in
parentheses is an aggregate value)
- Array access uses parens because arrays and functions are both mappings from value to value
Using Procedures and Functions (With Array Parameters) (sumcountprocs.adb)
- Demonstrates breaking CountPosZeroNeg into separate routines?
- Use procedures and functions to sum and count positives, etc.:
sumcountprocs.adb
and
prettified
- Things to note:
-
Order of declarations: constants and types, procedures and functions,
variables for main routine
- Must define before use!
- Variables declared inside procedures are called local variables
- Where are the variables for sumcountprocs?
- Functions are like java methods with non-void return types
- Procedures are similar to methods with void return type
- However, procedures can have out mode parameters that return
values to the caller (see below)
Parameter Modes (sumcountprocs.adb continued)
- Parameters have modes that specify the direction the values flow
- Possible parameter modes: In, Out, and In Out
- In: values passed from caller to called
- Out: values passed from called back to caller
- In Out: values passed from caller to called, and back!
- In Mode is the default
- Using a Structure Chart helps thinking about direction of
information flow
- What is the mode of the parameter for get?
Detecting Errors with Parameter Modes
- What kinds of parameter mode errors are caught:
- Attempting to assign a value to an in mode parameter
- Not assigning a value to an out mode parameter
- Attempting to change an in mode parameter by calling a procedure
- In mode parameters are treated like constants in the procedure:
procedure foo1(i: in Integer) is
begin
i := 3; -- error
Out mode parameters are uninitialized on entering the procedure:
procedure foo2(o: out Integer) is
j: Integer := o + 1; -- compiles, but erroneous
Out mode parameters must be given a value
procedure foo3(o: out Integer) is
begin
null;
end foo3; -- compiles but erroneous
Out mode parameters must have variables as actual parameters
procedure foo4(o: out Integer) is
begin
o := 3;
end foo4;
procedure foo5(i: in Integer) is
c: constant Integer := 5;
begin
foo4(2); -- compilation error
foo4(c); -- compilation error
foo4(i); -- compilation error
Procedures, Functions, and Parameter Modes
- Ada 2005 and earlier:
- Functions can have only in parameters
- Only procedures can use out mode and in out mode parameters.
- Later we will see that this can reduce side effects and
makes programs more reliable
- Ada 2012 allows out params for functions
- Why: Truth in advertising
- Side effects using possible using in mode pointers or globals
- In mode pointers can misleadingly advertise no side effects
- Safety feature: warns of dangerous dependencies (eg if f(x) and x = 0)
- Frequently used to modify a value and return a status
Filling the array using integer_text_io.Get (sumcountprocs2.adb)
Scope
- Basic rule: a declaration is visible from the declaration to
the end statement associated with the declaration section containing the
declaration
Global Variables
- Definition: A variable is global to a block if it is visible in the
block but not declared in the block
- Important rule: Avoid global variables
- Why: They increase coupling, making debugging difficult
- Need an example:
- Global constants and types are allowed
- Global variables are allowed in certain special cases (eg debug)