for example I have this code
--main procedure
Procedure Main Is
--If I declare a variable here will it be considered as
--global to the sub procedures?
--sub procedure
Procedure Sub
Begin
End Sub;
Begin
--If I declare a variable here will it be considered as
--global to the sub procedure?
End Main;
any help is appreciated.
Roger> Procedure Main Is
Roger> --If I declare a variable here will it be considered as
Roger> --global to the sub procedures?
Yes.
Roger> Begin
Roger> --If I declare a variable here will it be considered as
Roger> --global to the sub procedure?
The point is that you can't declare anything after a begin... (you may
open a declare block, but the scope of variables declared in it is
limited to this block).
--
Éric Jacoboni, né il y a 1339791698 secondes
It is a relative notion. In the 1960's computing or usual assembler you have
two levels: a global (Fortran's common), and a local. In Ada (like Pascal)
it is a question of nesting. For instance you can have a procedure inside a
package inside a procedure. You can rely on two rules:
1/ what is outside is visible from inside
2/ what is above is visible from below
They are valid not only for variables, but for every definition
(type, procedure, function, package, ...)
HTH
_____________________________________________________
Gautier -- http://www.mysunrise.ch/users/gdm/gsoft.htm
NB: For a direct answer, address on the Web site!
_________________________________________________________________
Unlimited Internet access -- and 2 months free! Try MSN.
http://resourcecenter.msn.com/access/plans/2monthsfree.asp
This is a good question. Unlike some languages, in which a variable is
either global or local, Ada has various degrees of locality. This
sometimes causes heated discussions between those who follow a strict,
blind-obedience rule of no global variables and those with a better
understanding of the matter.
For example, a variable declared in a package body may be global to the
subprograms in the package, but they are not visible outside the
package. Knee-jerk rule following says these are not allowed because
they're global variables. Actually, these are state variables for the
package and perfectly acceptable.
The only real global variables in Ada are those declared in the visible
part of package specifications. These are a no-no.
>
> for example I have this code
> --main procedure
> Procedure Main Is
> --If I declare a variable here will it be considered as
> --global to the sub procedures?
Yes.
> --sub procedure
> Procedure Sub
>
> Begin
>
> End Sub;
>
>
>
> Begin
> --If I declare a variable here will it be considered as
> --global to the sub procedure?
> End Main;
You can't declare a variable there without a block statement. Perhaps
you meant
begin -- Main
declare
-- variable declaration
begin
...
end;
end Main;
in which case you can have declarations there. Such declarations are not
visible outside the block statement in which they are declared, so they
are not visible to Sub.
Not that Ada is not C, so your main procedure can have a more
informative name than "Main" if you like.
--
Jeff Carter
"Monsieur Arthur King, who has the brain of a duck, you know."
Monty Python & the Holy Grail
procedure main is
V_main_1 : integer := 0;
procedure s1 is
v_s1_1 : integer := 0;
begin
null; -- V_main_1 and V_s1_1 is vissible here;
end;
begin --main
declare
V_Main_2 : integer := 0;
begin
null; -- V_main_1, V_main_2 and V_s1_1 is vissible here;
end;
-- V_main_1 is vissible here;
end main;
So think of it as a tree wher the main trunk is the "main" procedure and all
branches will see back towards the main trunk
but not other branches.
/Per Sandberg
"Roger" <i...@yourdreams.com> wrote in message
news:5Ldx9.3695$151....@weber.videotron.net...
Generally a bad idea. However, it's worth noting that
a large number of very smart people saw no reason to
make them illegal when the language was being designed.
I don't consider Ada to have global variables when used normally (at least
not in the COBOL sense I was brought up with), as even a data item declared
in a package specification, it is still local to that package - as in, you
still have to reference the package name somewhere before using the data
item.
The exception to this is "pragma Export" and "pragma Import". With these
you can do hideously dangerous things and completely remove the need for
context clauses! :-)
e.g.
package body Foo is
...
My_Local : Integer;
pragma Export (Ada, My_Local, "My_Local");
...
end Foo;
...
-- WITHOUT "with Foo;"
--
package body Bar is
...
-- No idea where "My_Local" is actual declared!
--
My_Other_Local : Integer;
pragma Import (Ada, My_Other_Local, "My_Local");
...
end Bar;
True. But since at the Ada launch on 1980 Dec 10, some of those people
said that the language contained "goto" primarily to facilitate
automated translation from languages such as FORTRAN 66, and "while"
primarily to facilitate such translation from languages such as Pascal,
we should not construe that everything allowed by the language is
necessarily a Good Idea for new software.
and they need not even be of the same type. You can completely undermine the Ada
type concept with this.
As Martin said: it's "hideous"
> True. But since at the Ada launch on 1980 Dec 10, some of those people
> said that the language contained "goto" primarily to facilitate
> automated translation from languages such as FORTRAN 66, and "while"
> primarily to facilitate such translation from languages such as Pascal,
On the "while" comment: are you (or were they) referring to the fact
that a while loop can be expressed as a loop/exit when/end loop
construction, or is there something about while loops that should be
avoided? Isn't there a preference hierarchy from for loop to while
loop to loop? I.e. use a for loop whenever possible, failing that a
while loop, and as a last resort a loop with an exit. OK, and as a
last last resort use a goto.
Fraser.
I don't know what the "founders" intended but how's this for a
rule: Use a for-loop only when it's appropriate, use a while-loop only
when it's appropriate, use an "infinite" loop in all other cases. :-)
Seriously, here's what I try to do: use the for-loop and while-loop
only when they express the loop's termination condition. In other
words, do not use exit statements within loops of these forms; if a loop
requires an exit statement, then use an "infinite" loop instead.
Unfortunately, my will power is often too weak to overcome the
convenience of the automatically declared counting variable of a
for-loop. I'm much better with while loops.
Charlie
--
For an email response, my real user name is csampson and my ISP is
inetworld.net.
Wow! I've never tried anything _that_ dangerous before!!!
> ... Isn't there a preference hierarchy from for loop to while
> loop to loop? I.e. use a for loop whenever possible, failing that a
> while loop, and as a last resort a loop with an exit. OK, and as a
> last last resort use a goto.
In my style guide, I use a for loop _only_ if the loop will _always_
be executed exactly as indicated in the "for" part. No other loop
exits allowed.
Otherwise, I use an "exit when" loop.
Never use a "while" loop. Mostly because "exit when" is much clearer,
partly because I've used other languages that had slightly different
definitions of "while", and I can't keep them straight.
--
-- Stephe
In Pascal, a for loop was always done entirely; you have to be aware that
Ada is different in that respect, but that does not mean that you should not
use a more powerful construct when you can.
--
---------------------------------------------------------
J-P. Rosen (ro...@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
The point made by Ichbiah, Barnes, and Firth at the Ada Launch was that
while loops express the condition for continuation, not exit, and this
is difficult for beginners to understand, and they usually require the
use of negative logic (while not End_Of_File), which is often more
difficult to understand than positive logic; that a loop with an exit as
its first statement expresses the condition for exit, and this is easy
for everyone to understand, and it usually uses positive logic (exit
when End_Of_File). For these reasons they thought the latter form should
be prefered.
There are as many preference hierarchies as there are people creating
preference hierarchies. Here's one: use assembler whenever possible,
failing that use C, failing that use a C derivative, failing that use
another language other than Ada, and use Ada when forced to at gunpoint.
I don't happen to agree with it, but I suspect that there are those who
do. Just because you've seen someone's preference hierarchy doesn't mean
you should follow it.
I use the rule that you should use the appropriate form of a loop for
the iteration that's required. If you accept the arguments of "the
founders", while is never the appropriate form.
--
Jeff Carter
"We burst our pimples at you."
Barring a goto. Judicious use of gotos can sometimes make loops in
Pascal much easier to read and understand.
--
Jeff Carter
"We burst our pimples at you."
>In Pascal, a for loop was always done entirely;
Don't know what the current standard says exactly, but modern Pascal
versions know a break-"instruction". Just to mention, doesn't matter.
Vinzent.
> Never use a "while" loop. Mostly because "exit when" is much clearer,
> partly because I've used other languages that had slightly different
> definitions of "while", and I can't keep them straight.
Like what? I mean, which languages have different definitions of
"while" loops, and what are the differences. I thought "while" loops
were one of the few cases where there is pretty much agreement on the
semantics across languages.
I like while loops, and use them in favor of loop/exit whenever the exit
condition happens at the top.
I seem to recall some issue in SPARK, where they discouraged while
loops. Something about wanting to put a loop-invariant before the exit
condition? Maybe one of the SPARK folks can comment on that. I'm happy
to write assertions that help program-proving tools, if they also help
me, the human reader (or at least neutral to the human reader), but I'm
a bit uncomfortable using a style that helps tools, but hinders humans.
- Bob
I'm not sure exactly what that means. What French word
were you thinking of for "entirely" ?
Anyway, all the versions of Pascal I know of allow:
FOR iter DO stmt
REPEAT stmt UNTIL cond
WHILE cond DO stmt
REPEAT
Statements
WHILE Condition
or UNTIL Condition
Larry
> Like what? I mean, which languages have different definitions of
> "while" loops, and what are the differences. I thought "while" loops
> were one of the few cases where there is pretty much agreement on the
> semantics across languages.
>
Well, it isn't a while, but...
repeat
...
until <cond>
which has the situation that it exits when <cond> goes true, whereas
while <cond>
...
wend
continues looping on a true <cond>.
And /neither/ structure really handles the form where you need a bit
of processing before you can do the test...
loop
preprocessing which sets flag
exit when <cond>
other processing
end loop.
Using just a while loop, as in many languages, the above turns into:
(initial) preprocessing which may set flag
while not flag
other processing
preprocessing which sets flag
wend
--
> ============================================================== <
> wlf...@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulf...@dm.net | Bestiaria Support Staff <
> ============================================================== <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <
>>
> If I remember correctly, the Fortran compiler for the SDS/Xerox Sigma
> computers used:
>
> REPEAT
> Statements
> WHILE Condition
>
> or UNTIL Condition
>
Ooooh... My reference manuals are in storage so I can't check... Of
course, this was the same compiler that defined a "GLOBAL vars"
statement... Which effectively translated:
global x, y, m, n, a
into
common /x/ x
common /y/ y
common /m/ m
common /n/ n
common /a/ a
IE, /each/ variable named generated a named common block containing
only that variable, and the common block name matched the variable name.
If it were a variable the program could change the value with the assignment
statement or by passing it as an out or in out parameter to a procedure.
This is forbidden and will be reported as an error by any validated Ada
compiler.
--
R. Tim Coslet
r_tim_...@pacbell.net
Technology, n. Domesticated natural phenomena.
> From: clav...@grzorgenplatz.net (Charles H. Sampson)
> Organization: NetHere Inc.
> Newsgroups: comp.lang.ada
> Date: Tue, 5 Nov 2002 05:53:55 -0800
> Subject: Re: Local vs global variables in ADA
>
[...]
AKA the "Read Ahead Principle" so beloved of Jackson Structured Programming
and COBOL-heads.
It wasn't that bad when the preprocessing bit was farmed off to a subpogram
and you wrote your programs using a decent JSP tool.
> Like what? I mean, which languages have different definitions of
> "while" loops, and what are the differences. I thought "while" loops
> were one of the few cases where there is pretty much agreement on the
> semantics across languages.
>
> I like while loops, and use them in favor of loop/exit whenever the exit
> condition happens at the top.
Of course the exit has a negated condition compared to the while, so
this may make it easier to read in some situations.
while (not finished) and (not found) loop
...
end loop
or
while not (finished or found) loop
or
loop
exit when finished or found;
...
end loop;
Dale
Of course, I'm talking about "original" Pascal (Wirth), not languages that
have considerable extensions like Turbo.
Which, technically, is the only correct way to go...
In Fortran, the common name is global, the variable
names in the common are local names. Fortran has
no global variable names.
--
Cheers!
Dan Nagle
Purple Sage Computing Solutions, Inc.
On Tue, 05 Nov 2002 18:19:04 -0800, Dennis Lee Bieber
<wlf...@ix.netcom.com> wrote:
<snip requoted>
>>
> Ooooh... My reference manuals are in storage so I can't check... Of
>course, this was the same compiler that defined a "GLOBAL vars"
>statement... Which effectively translated:
>
> global x, y, m, n, a
>
>into
>
> common /x/ x
> common /y/ y
> common /m/ m
> common /n/ n
> common /a/ a
>
>IE, /each/ variable named generated a named common block containing
>only that variable, and the common block name matched the variable name.
<snip sig>
I see now. "Entirely" as in the
the _entire_ loop with no early exit.
Sorry about misunderstanding.
I thought you meant "only" as
as in "no other kind of loop"
"Entirely" is a reasonable
word to use there, although
"completely" might have been
slightly better.
Well, you could use a function to get the while test value.
Couldn't you?
Not really - while loops are fine in SPARK. You can place an assertion
either after the implicit loop exit, thus:
while A loop
--# assert ... ;
S;
end loop;
or before it:
while A
--# assert ... ;
loop
S;
end loop;
or anywhere else in the loop body for that matter. Note that the first form
does result in an additiona basic path (bypasses the assertion) for the
zero-iterations case.
The rules for placing exit statements in SPARK ensure that the resulting
flow graph is both reducible and can be generated from a semi-structured
graph grammar. This property makes subsequent analyses (particularly
info flow analysis and verification condition generation) tractable.
- Rod
> Stephen Leake <stephen....@gsfc.nasa.gov> writes:
>
> > Never use a "while" loop. Mostly because "exit when" is much clearer,
> > partly because I've used other languages that had slightly different
> > definitions of "while", and I can't keep them straight.
>
> Like what? I mean, which languages have different definitions of
> "while" loops, and what are the differences. I thought "while" loops
> were one of the few cases where there is pretty much agreement on the
> semantics across languages.
I had hoped someone else would actually answer your question, but they
didn't.
At the moment, I only use Ada and C, so I don't really remember what
the issues are precisely.
What I do remember is being confused about whether the loop body
executes once for a "false" while condition. Especially when the
"while" is at the end.
I believe different languages (I've used Pascal, C, COBOL, Fortran,
lisp, Ada) do this differently. But I could be wrong; it could be that
I could just never remember the (common) rule.
Hmm. Checking my ANSI C manual, I see that if "while" is at the top,
the loop body is not executed if the condition is false. If "while" is
at the bottom, the loop body is executed once if the condition is
false.
Ada only allows "while" at the top, and it has the same rule as C.
Anyway, I see no reason to strain my limited supply of brain cells
trying to remember this, when loop/exit/end loop covers all the cases,
and is perfectly clear.
--
-- Stephe
Most of my experience is in FORTRAN, avoiding vendor specific
extensions, but still... writing whole function WITH SIDE-EFFECTS and
calling overhead to change
read(unit,format, iostat=ios) v1, v2, v3
10 if (ios .ne. EOF_VALUE) then
other processing
read(unit,format, iostat=ios) v1, v2, v3
goto 10
endif
into
integer function funcwithsideeffects(a1,a2,a3)
read(unit,format, iostat=is) a1, a2, a3
funcwithsideeffects = is
return
...
10 if (funcwithsideeffects(v1,v2,v3) .ne. EOF_VALUE) then
other processing
goto 10
endif
Would have gotten jeers from code inspections at my former employer.
Especially as I'd have to document/justify the creation of those
snippet functions.
As it is, I only used "goto" in constructs where the destination was
before the goto (as anyone reading the code should have seen the target
already, and not have to search). This is why I did not fake the Ada
loop/exit when/end loop in FORTRAN... If I had, the result would have
looked like:
10 continue
read(unit,format, iostat=ios) v1, v2, v3
if (ios .eq. EOF_VALUE) goto 20
other processing
goto 10
20 continue
Granted, Ada's I/O system is such that creating a procedure to
consolidate the three variable reads into one is feasible -- but again,
to put the status return into it still requires a function with
side-effects if one wants to use while ... loop/end loop
> Hello,
>
> Which, technically, is the only correct way to go...
>
> In Fortran, the common name is global, the variable
> names in the common are local names. Fortran has
> no global variable names.
>
Yeah, but when each such common block is allocated a full page (or
whatever size the linker/OS swapper uses) it gets a bit wasteful of
memory (the physical memory of my college Sigma-6 was only around
1MByte, and had to support ~50 active user terminals... 50 "global"
integers would have taken 25KBytes of memory on page alignments).
--
> As it is, I only used "goto" in constructs where the destination was
> before the goto (as anyone reading the code should have seen the target
> already, and not have to search). ...
Hmm. I have always believed just the opposite: that gotos that jump
forward (and outward) are easier to understand that gotos that jump
backward (and therefore form a loop).
For example, I did a lot of Pascal programming a long time ago, and I
don't think I *ever* wrote a backward-jumping goto. But I wrote quite a
few loop-exiting gotos (since Pascal doesn't have Ada's "exit" or
"return"). And quite a few "while True do...", since Pascal doesn't
have Ada's plain "loop".
- Bob
"Never use gotos, and the exception confirms the rule"
:-)
Isn't this the classic Pascal idiom
while not <cond> do begin
preprocessing which sets flag
if not <cond> then begin
other processing
end {if};
end {while};
?
--
Jeff Carter
"If I could find a sheriff who so offends the citizens of Rock
Ridge that his very appearance would drive them out of town ...
but where would I find such a man? Why am I asking you?"
Blazing Saddles
> I believe different languages (I've used Pascal, C, COBOL, Fortran,
> lisp, Ada) do this differently. But I could be wrong; it could be that
> I could just never remember the (common) rule.
>
> Hmm. Checking my ANSI C manual, I see that if "while" is at the top,
> the loop body is not executed if the condition is false. If "while" is
> at the bottom, the loop body is executed once if the condition is
> false.
>
Or, equivalently but clearer to me, the test is executed at the bottom
of the body.
> Ada only allows "while" at the top, and it has the same rule as C.
>
(Modern) Fortran has DO WHILE which does test at top (before),
which is really just sugar for DO (forever) + IF NOT cond EXIT,
in addition to the traditional arithmetic DO = other langs FOR.
Early Fortran (66) had the infamous one-trip-do-loop: if you
wrote an empty range e.g. DO I=1,0 it was officially undefined
and some (most?) compilers would execute the body once
before testing at the end, as an optimization -- especially on
S/360 using BXLE. Modern Fortran requires skipping it.
PL/1 has an amalgamated DO where you can write
an arithmetic range (or at least sequence?) plus a
boolean test at top/before in the same statement!
I vaguely recall algol allowing something similar,
but never used it because it was too confusing.
As already noted, Pascal has WHILE cond DO body
and REPEAT body UNTIL cond with the sense inverted
apparently to make them more obviously different,
as well as FOR.
COBOL (85) is the worst, for my money. For inline loops
the test always appears at the top of a PERFORM construct
but you can specify either WITH TEST { BEFORE | AFTER }.
This may(?) derive from earlier COBOL having *only* out-of-line
loops -- PERFORM that-code-over-there UNTIL cond , or FOR ~
PERFORM blah VARYING i FROM 1 BY 2 UNTIL i > 9 --
where there is no body to be on the top or bottom of.
Well, actually there's one thing worse -- perl. You can read
through 20 lines of code thinking it's an imperative statement
and only at the end discover it's a loop.
> Anyway, I see no reason to strain my limited supply of brain cells
> trying to remember this, when loop/exit/end loop covers all the cases,
> and is perfectly clear.
>
Your choice.
--
- David.Thompson 1 now at worldnet.att.net