Options: Use Forum View Use Proportional Font Show Text Part by Default Show All Mail Headers Message: [<< First] [< Prev] [Next >] [Last >>] Topic: [<< First] [< Prev] [Next >] [Last >>] Author: [<< First] [< Prev] [Next >] [Last >>]

 Subject: Re: Nesting (was Static variables in Ada) From: "W. Wesley Groleau x4923" <[log in to unmask]> Reply To: W. Wesley Groleau x4923 Date: Mon, 14 Dec 1998 11:02:50 -0500 Content-Type: text/plain Parts/Attachments: text/plain (57 lines)
```>     The nesting issue really involves nested variable declarations.
> Sometimes variables do need to be nested:
>
>     function Get_Matrix return Matrix is
>       Number_of_Rows, Number_of_Columns: Integer;
>       ...
>     begin
>       Get(Number_of_Rows);
>       Get(Number_of_Columns);
>       declare
>          Result: Matrix(Number_of_Rows, Number_of_Columns) := (others => 0);
>       begin
>          for I in 1..Number_of_Rows loop
>            for J in 1..Number_of_Columns loop
>              Get(Result(I,J));
>            end loop;
>          end loop;
>       end;
>     end Get_Matrix;
>
>     But, and this is the key, such nesting almost always involves delcare
> blocks, not subprograms, or not just subprograms.

On the other hand, Get could be a function....

function Get (Prompt : String := "") return Integer is ....

function Get_Matrix return Matrix is

Result: Matrix (1 .. Get ("Rows:    "),
1 .. Get ("Columns: "));

begin

for I in Matrix'Range(1) loop
for J in Matrix'Range(2) loop

Result(I,J) :=
Get ("Value(" & Integer'Image (I) & ","
Integer'Image (J) & "): " );
end loop;
end loop;

return Result;  -- don't forget this...

end Get_Matrix;

And some of that input could easily be "lifted" to

function Get_Matrix ( Rows, Columns : Integer ) return ....

I find declare blocks valuable aids to localization, information hiding,
and sometimes for readability.  However, if they are unnecessary, they can
act as "clutter," obscuring more important stuff.
```