Board index » delphi » Passing Paramaters

Passing Paramaters

Example:

procedure SomeProc1(X: Integer);
begin
  X := 5;
end;

procedure SomeProc2(var X: Integer);
begin
  X := 5;
end;

var
  X: Integer;
begin
  X := 3;
  SomeProc1(X);
  // X is still 3
  SomeProc2(X);
  // X is now 5
end;

"Michael Stevens" <steve...@infoserv.pentech.ac.za> schreef in bericht
news:8p845m$2iov$1@news.adamastor.ac.za...

Quote
> Hi...
> When you pass paramaters, what is the difference between:
> procedure SomeProc(X: integer)   and
> procedure SomeProc(var X: integer)

> Thanks

 

Re:Passing Paramaters


With SomeProc(X: integer); an VALUE is send to the procedure. You can use
variables or constant values as parameter e.g. SomeProc(variable) or
SomeProc(1024).
With SomeProc(var X : integer); an ADRESS is send to the procedure. The
adress points to an variable. So if the parameter is changed it changes also
this variable. Here you can only use variables e.g. SomeProc(i).

Michael Stevens <steve...@infoserv.pentech.ac.za> schrieb in im Newsbeitrag:
8p845m$2io...@news.adamastor.ac.za...

Quote
> Hi...
> When you pass paramaters, what is the difference between:
> procedure SomeProc(X: integer)   and
> procedure SomeProc(var X: integer)

> Thanks

Re:Passing Paramaters


Quote
"Michael Stevens" <steve...@infoserv.pentech.ac.za> wrote in message

news:8p845m$2iov$1@news.adamastor.ac.za...

Quote
> Hi...
> When you pass paramaters, what is the difference between:
> procedure SomeProc(X: integer)   and
> procedure SomeProc(var X: integer)

In the help file table of contents you will find a section entitled Object
Pascal Reference. I would suggest that you read it. In particular you will
find a chapter devoted to Procedures and Functions. This chapter has an
entire section, titled Parameters, devoted, in part, to answering your
question.

BTW I commend the Object Pascal Reference to all readers. It is reasonably
good at answering most questions concerning the Delphi Pascal
implementation.

Re:Passing Paramaters


In a nutshell, you can declare passed parameters in three ways:

procedure DoThis(X: SomeType);

  Delphi allocates stack space and copies the original value
  to X. You can change the value of X at will without affecting
  the original variable.

procedure DoThis(const X: SomeType);

  Delphi allocates stack space and copies the original value
  to X. You can NOT change the value of X. This method produces
  more efficient code.

procedure DoThis(var X: SomeType);

  Delphi does not allocate stack space, but treats X as a pointer
  to the original variable. Changing the value of X immediately
  changes the original variable. (Passing a constant variable in
  this case will result in a compiler error.)

-- Erwin Dokter

Quote
Michael Stevens wrote:

> Hi...
> When you pass paramaters, what is the difference between:
> procedure SomeProc(X: integer)   and
> procedure SomeProc(var X: integer)

> Thanks

Re:Passing Paramaters


Quote
"Erwin Dokter" <e.dokte...@kpn.com> wrote in message

news:39B8C7C4.B9121947@kpn.com...

Quote
> In a nutshell, you can declare passed parameters in three ways:
> procedure DoThis(var X: SomeType);

>   Delphi does not allocate stack space, but treats X as a pointer
>   to the original variable. Changing the value of X immediately
>   changes the original variable. (Passing a constant variable in
>   this case will result in a compiler error.)

Its true that Delphi doesn't allocate stack space for an integer but it does
have to allocate space for the address of (pointer to) the actual parameter.

Other Threads