Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What difference does it make when I use "const" in a procedure's parameter?

What difference does it make when I use a const parameter in a procedure?

Take the following procedure for example:

procedure DoSomething(Sender: TObject; const Text: String; var Reply: String);
begin
  //Text is read-only and Reply will be passed back wherever DoSomething() was called
  Reply:= Text;
end;

The parameter Text: String is prefixed with const so that (as far as I know), a copy of the value is made and used - and is read-only. What I was wondering is how is does this affect the application any differently than if I didn't put const there? Perhaps a performance trick?

like image 779
Jerry Dodge Avatar asked Jun 12 '12 17:06

Jerry Dodge


People also ask

What is the effect of the const keyword when used with a function parameter?

C++ solution: constant parameters. When you put "const" in front of a parameter, it means that it cannot be modified in the function.

What does it mean to pass a parameter to a function as const?

A constant parameter, declared by the keyword const , is a read-only parameter. This means that we can not modify the value of the constant parameter in the function body. Using the const keyword tells the compiler that the value of that parameter will not be changed inside the function.

What does const mean Delphi?

A const parameter clearly tells the reader that the subroutine does not change the parameter's value. This improves the readability and clarity of the code. The compiler enforces the restriction. If you accidentally try to assign a new value to a const parameter, the compiler issues an error message.

Why the const function argument is necessary?

Always use const on function parameters passed by reference or pointer when their contents (what they point to) are intended NOT to be changed. This way, it becomes obvious when a variable passed by reference or pointer IS expected to be changed, because it will lack const .


1 Answers

Looking at the documentation states:

"Using const allows the compiler to optimize code for structured - and string-type parameters. It also provides a safeguard against unintentionally passing a parameter by reference to another routine."

In case of a string for example the optimization means there is no additional refcounting when passing as const. Also passing as const does not mean it's a copy. Often it internally passes as reference because the compiler ensures no write access to it.

Some very interesting articles to completly understand what's going on under the hood:

http://delphitools.info/2010/07/28/all-hail-the-const-parameters

http://vcldeveloper.com/articles/different-function-parameter-modifiers-in-delphi

Edit:

A simple example to show that const may result in pass by reference internally:

program Project1;

{$APPTYPE CONSOLE}

type
  PMyRecord = ^TMyRecord;
  TMyRecord = record
    Value1: Cardinal;
    Value2: Cardinal;
  end;

procedure PassAsConst(const r: TMyRecord);
begin
  PMyRecord(@r).Value1 := 3333;
  PMyRecord(@r).Value2 := 4444;
end;

procedure PassByVal(r: TMyRecord);
begin
  PMyRecord(@r).Value1 := 3333;
  PMyRecord(@r).Value2 := 4444;
end;

var
  r: TMyRecord;
begin
  r.Value1 := 1111;
  r.Value2 := 2222;
  PassByVal(r);
  Writeln(r.Value1);
  Writeln(r.Value2);

  PassAsConst(r);
  Writeln(r.Value1);
  Writeln(r.Value2);

  Readln;
end.
like image 117
Stefan Glienke Avatar answered Sep 29 '22 14:09

Stefan Glienke