Board index » delphi » what does var do in the parameter passing

what does var do in the parameter passing

I wanted to know what 'var' does in parameter passing for function or
procedure
like eg. Function FunType(var ch: integer):integer;

now whats the difference if i dont give 'var'????
please let me know....thanx....

 

Re:what does var do in the parameter passing


In article <7jrh3i$rn...@news.vsnl.net.in>,
  "Ramrao Salkar" <ramr...@vsnl.com> wrote:

Quote
> I wanted to know what 'var' does in parameter passing for function or
> procedure
> like eg. Function FunType(var ch: integer):integer;

> now whats the difference if i dont give 'var'????
> please let me know....thanx....

This is so elementary, so RTFM or ask your tutor!

Robert
--
Robert AH Prins
prin...@williscorroon.com

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Re:what does var do in the parameter passing


Quote
Ramrao Salkar wrote:

> I wanted to know what 'var' does in parameter passing for function or
> procedure
> like eg. Function FunType(var ch: integer):integer;

> now whats the difference if i dont give 'var'????
> please let me know....thanx....

If you put 'var' in front of a parameter, you will be able to change the
value of that parameter withing the procedure or function. Normally you
are also able to do this, but when the procedure or function exits the
value will be set back to what it was before you called the prodedure.
This will not be the case when you use var.

greetz,

----------------------------------
toDSaH
Email: tod...@todsah.nl.eu.org
Homepage : http://todsah.nl.eu.org
----------------------------------

Re:what does var do in the parameter passing


Quote
Ramrao Salkar wrote:
> I wanted to know what 'var' does in parameter passing for function or
> procedure
> like eg. Function FunType(var ch: integer):integer;

When you specifiy "var", the compiler does not pass the value stored
in a variable to the sub program, it rather passes a pointer to that
variable. That's also why you cannot pass an immediate value to such
a sub program. In your example, the compiler does not pass the value
in ch, it passes a pointer to ch ("call by reference" instead of "call
by value").

As a result, when you write ch := 2; inside the sub program, this
change also takes effect in the main program (that is, when you leave
the procedure, the variable you specified as the parameter to the sub
program will be 2 also in the main program). If you had not specified
"var" in the sub program header, ch would just be a local variable which
wouldn't have anything to do with the main program.
In the following program, you can consider ch to be some sort of alias
for a:

var
  a : integer;

procedure test(var ch: integer);
begin
  ch := 2
end;

begin
  { The value of a is currently undefined }
  test(a)
  { Now, the value of a is 2 }
end.

If you omit "var", you can still assign a value to ch inside the
procedure, but it will be lost on exit and a will not be affected.

It sounds more intricate than it actually is. Maybe someone else
can explain it more precisely.

--
Messages not posted via IVM or RemarQ are not from me.
Oh, and I don't speak Chinese.

Re:what does var do in the parameter passing


Quote
Ramrao Salkar wrote:

> I wanted to know what 'var' does in parameter passing for function or
> procedure
> like eg. Function FunType(var ch: integer):integer;

There are two answers. Language definitions state that an assignment to
CH within the procedure is equivalent to an assignment to the
corresponding actual parameter.For example if there is a call
Funtype(x), ch:='a' within the procedure means the same as x:='a';

Many Pascal compilers, including Turbo Pascal, implement this behaviour
by passing a pointer to the procedure and assigning the value to the
address of the pointer. Without strict control of actual-formal
correspondence, this may lead to strange results. For example, if the
call Funtype('x') would be allowed, ch:='y' in the procedure would
result in 'x' getting the value 'y'.

--
--------------------------------------------
Rommert J. Casimir
Tilburg University, Room B435
P.O. Box 90153
5000LE Tilburg, The Netherlands
tel 31 13 4662016, fax 31 13 4663377
http://cwis.kub.nl/~few/few/BIKA/rc_home.htm
---------------------------------------------

Re:what does var do in the parameter passing


Quote
Robert AH Prins wrote:
> This is so elementary, so RTFM or ask your tutor!

Personally, instead of posting this I would rather have posted nothing.

--
Messages not posted via IVM or RemarQ are not from me.
Oh, and I don't speak Chinese.

Re:what does var do in the parameter passing


In article <376103F9.67AFC...@todsah.nl.eu.org>,

Quote
toDSaH  <tod...@todsah.nl.eu.org> wrote:
>Ramrao Salkar wrote:

>> I wanted to know what 'var' does in parameter passing for function or
>> procedure
>> like eg. Function FunType(var ch: integer):integer;

>> now whats the difference if i dont give 'var'????
>> please let me know....thanx....

>If you put 'var' in front of a parameter, you will be able to change the
>value of that parameter withing the procedure or function. Normally you
>are also able to do this, but when the procedure or function exits the
>value will be set back to what it was before you called the prodedure.
>This will not be the case when you use var.

That is total nonsense. No values are set back etc. Pascal has two ways
of passing a parameter: by value or by reference. When a parameters is
passed by value the value of the parameter is pushed to the stack and
the procedure then can use that value like a local variable. When a
parameter is passed by reference (var) then the address of the
actual parameter is passed instead of the value. When the parameter is
used in the procedure the an implicit pointer reference is done and the
variable that was passed is accessed. One can only pass variables (or
typed constants) as variable parameters.

TP 7.0 has also a third type: constant parameters (keyword const) as the
name says they are constants within the procedure. This means the actual
implementation can be either value or reference. In practice simple
types are passed by value and complex ones (arrays, records, strings,
sets) by reference (with minor exceptions). Const parameters are
especially nice with strings as one can pass a string constant to them.

Osmo

Re:what does var do in the parameter passing


Quote
Osmo Ronkanen wrote:

> In article <376103F9.67AFC...@todsah.nl.eu.org>,
> toDSaH  <tod...@todsah.nl.eu.org> wrote:
> >Ramrao Salkar wrote:

> >> I wanted to know what 'var' does in parameter passing for function or
> >> procedure
> >> like eg. Function FunType(var ch: integer):integer;

> >> now whats the difference if i dont give 'var'????
> >> please let me know....thanx....

> >If you put 'var' in front of a parameter, you will be able to change the
> >value of that parameter withing the procedure or function. Normally you
> >are also able to do this, but when the procedure or function exits the
> >value will be set back to what it was before you called the prodedure.
> >This will not be the case when you use var.

> That is total nonsense. No values are set back etc. Pascal has two ways
> of passing a parameter: by value or by reference. When a parameters is
> passed by value the value of the parameter is pushed to the stack and
> the procedure then can use that value like a local variable. When a
> parameter is passed by reference (var) then the address of the
> actual parameter is passed instead of the value. When the parameter is
> used in the procedure the an implicit pointer reference is done and the
> variable that was passed is accessed. One can only pass variables (or
> typed constants) as variable parameters.

> TP 7.0 has also a third type: constant parameters (keyword const) as the
> name says they are constants within the procedure. This means the actual
> implementation can be either value or reference. In practice simple
> types are passed by value and complex ones (arrays, records, strings,
> sets) by reference (with minor exceptions). Const parameters are
> especially nice with strings as one can pass a string constant to them.

> Osmo

In this case (a obvious newbie question) I thought I would keep it kind
of basic. If this guy (Ramrao) doesn't yet understand what var infront
of a parameter does , he probably wouldn't know what a pointer is. So
maybe it isn't a technically correct awnser but he should be able to get
the point.

greets,

----------------------------------
toDSaH
Email: tod...@todsah.nl.eu.org
Homepage : http://todsah.nl.eu.org
----------------------------------

Re:what does var do in the parameter passing


Hallo Rommert!

Quote
> Many Pascal compilers, including Turbo Pascal, implement this behaviour
> by passing a pointer to the procedure and assigning the value to the
> address of the pointer. Without strict control of actual-formal
> correspondence, this may lead to strange results. For example, if the
> call Funtype('x') would be allowed, ch:='y' in the procedure would
> result in 'x' getting the value 'y'.

Pascal knows two ways of passing arguments to procedures or functions: by
value and by reference. To indicate that a parameter has to be passed by
reference, you must use the keyword 'var'.

The character literal 'x' is a value in Pascal. And references to values do
not make sense! (Technically the character constant is stored in memory
anywhere, but for Pascal it is only a value.) This behavior is underlined
by the keyword 'var' that is also used to declare variables, i. e. areas in
memory that can contain values of a specific type.

I do not know the official Pascal report, but I am sure that people have
considered that values cannot be used as by-reference arguments.

Bj?rn

Re:what does var do in the parameter passing


Quote
> > >If you put 'var' in front of a parameter, you will be able to change the
> > >value of that parameter withing the procedure or function. Normally you
> > >are also able to do this, but when the procedure or function exits the
> > >value will be set back to what it was before you called the prodedure.
> > >This will not be the case when you use var.

> > That is total nonsense. No values are set back etc. Pascal has two ways
> > of passing a parameter: by value or by reference. When a parameters is
> > passed by value the value of the parameter is pushed to the stack and
> > the procedure then can use that value like a local variable. When a
> > parameter is passed by reference (var) then the address of the
> > actual parameter is passed instead of the value. When the parameter is
> > used in the procedure the an implicit pointer reference is done and the
> > variable that was passed is accessed. One can only pass variables (or
> > typed constants) as variable parameters.

> > TP 7.0 has also a third type: constant parameters (keyword const) as the
> > name says they are constants within the procedure. This means the actual
> > implementation can be either value or reference. In practice simple
> > types are passed by value and complex ones (arrays, records, strings,
> > sets) by reference (with minor exceptions). Const parameters are
> > especially nice with strings as one can pass a string constant to them.

> > Osmo

> In this case (a obvious newbie question) I thought I would keep it kind
> of basic. If this guy (Ramrao) doesn't yet understand what var infront
> of a parameter does , he probably wouldn't know what a pointer is. So
> maybe it isn't a technically correct awnser but he should be able to get
> the point.

Obviously your answer to the original question was not technically correct, and
you knew that, of course. Usually it is a good way to describe only the effects
within going into details, especially if the asking person does not know about
that details.

But your answer was not only technically incorrect, but also the described
effect is wrong.

Consider we could use your way of passing arguments indicated by the keyword
'var2':

procedure p;
    var
        i: integer;

    procedure print;
        begin writeln('i = ', i) end;

    procedure svar(var n: integer);
        begin n := n + 1; print end;

    procedure svar2(var2 n: integer);
        begin n := n + 1; print end;

    procedure sval(n: integer);
        begin n := n + 1; print end;

    begin
        i := 4; svar(i);  { i has become 5, test has printed 5. }
        i := 4; svar2(i);  { i is still 4, but test has printed 5. }
        i := 4; sval(i)   { i is still 4, test printed 4. sval's behavior is
different from svar2's! }
    end;

Bj?rn

Other Threads