[comp.lang.misc] VAR vs. IN/OUT

sommar@enea.se (Erland Sommarskog) (08/11/89)

In the on-going Pascal-Ada war Eugene Schwartzman (genesch@aplvax.jhuapl.edu) 
compares Ada's IN/OUT with Pascal's VAR:
>#Ada
>#is better for students, because many problems I encounter are people who
>#cannot quite remember when to use 'var' in a paramater declaration.  'in'
>#and 'out' are very obvious.
>
>       Well, then they are just plain stupid and don't deserve to be in
>       programming to begin with.  The first programs I wrote all used
>       'var', we didn't even get into value passing until we ran into
>       recursion.  With Ada, you need to remember when to use in/out and
>       when not too, granted it is very obvious, but even still with them
>       you have in, out, in/out combinations, with 'var' you have just one
>       'VAR'.

So you mean that the beginner's shouldn't learn the differene between
different parameter modes? But how to avoid that they by accident modify
their inparameters? And how do you explain that they never can have
expression as actual parameters, but always must have a temporary variable 
for that. God! Are there really places teaching programming like this?
Yes, in Ada you must know when to use IN, OUT or IN OUT, but for heaven's
sake the distinction between those modes are compulsary in beginner's
programming course, no matter which language you are using. Ada just happen 
to have very suitable notation.
  When I learnt Pascal as a freshman (well, we had some Fortrans first)
we were told something like you use VAR when you want the parameter to
be modified, or if you are passing a large parameter to improve
performance. Confusing if anything.
  (And besides your ridiculous argument doesn't hold a second. If you
really want one passing mode only, fine. Just declare all parameters
IN OUT. Not to recommend, but neither would I recommend *any* Pascal
programmer to declare all his variables as VAR.)
-- 
Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se
"Hey poor, you don't have to be Jesus!" - Front 242

pattis@june.cs.washington.edu (Richard Pattis) (08/12/89)

I published an article in SIGCSE Volume 20, Number 1 (February 1988) that
surveyed 40 CS-1/CS-2 books that used Pascal as their language of discourse.
Of those books that included a subprogram for binary searching, 50% passed
the parameter as non-VAR.  Thus, the running times of all these subprograms
was really O(N) not O(Log N) for all Pascal compilers I know; in fact, some
programs were recursive, which increased their running time to O(N Log N).
Similarly, the space requirements we also increased by this amount.

  In Ada (most compilers I know) parameter transmission is O(1), regardless
of the parameter mode.  The use of a correct parameter mode is subject to
static semantic rules.  A student cannot accidentally omit VAR and then find
that the actual parameter matching this formal remains unchanged after the
subprogram call.  Such inconsistent uses of Ada parameter modes are detected
and reported by Ada compilers.

  The equivalent of Pascal's non-VAR mode (which is rarely needed) can be
accomplished in Ada by declaring a local object and initializing it to the
value of an IN parameter (most useful for nonrecursive traversing of linked
data structures).

  Finally, Modula-3 and Turing each allow a "constant" (I think) parameter
mode that matches the semantics of Ada's IN mode.

Rich Pattis

sommar@enea.se (Erland Sommarskog) (08/14/89)

Richard Pattis (pattis@june.cs.washington.edu) writes:
>  The equivalent of Pascal's non-VAR mode (which is rarely needed) can be

Depends on what you mean. For efficiency VAR gives better result,
but VAR/non-VAR is the only way to distinguish between IN parameters
on the one hand and OUT and IN OUT parameters. Good programming
style is to only use VAR for the latter two cases. Only when
efficency is a problem IN parameters should be passed with the
VAR mechanism. (Or if you like me is using VAX-Pascal and CDD.
(Commin Data Dictionary) When you declare a quadword field in
the CDD, this is expanded in Pascal to an empty record with the
size attribute of 8 bytes. This type must always be a VAR parameter.
As a vaule parameter, this always results in a zero being passed. 
Have you ever such a stupid !"#$%&/() behaviour?)

But, of course, as well already have noted, this bitching with
passing mechanism isn't something the programmer should be
burdend with (and least of all a beginner). He should only be
concerned about the semantical meaning: IN, OUT or IN OUT.
                     -- 
Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se
"Hey poor, you don't have to be Jesus!" - Front 242