I.T. Discussion Community!
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

April Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Basics   Print This     
  From the September 2015 Issue of Prestwood eMag
Delphi Language Basics:
Delphi Parameters (var, const)
Posted 11 years ago on 12/21/2008 and updated 9/24/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Parameters


Delphi Parameters

Object Pascal allows parameters of the same type to be listed together, separated by commas, and followed with a single data type (more params of different data types can follow, after a semi-colon).

Delphi also supports default parameters (a form of overloading).

By Reference or Value (and by constant): The default for parameters is by value. For by reference, add var in front of the parameter. Object Pascal also offers constant parameters where you add const in front of the parameter. A constant parameter is like a local constant or read-only parameter the compiler can optimize. You cannot assign a value to a constant parameter, nor can you pass one as a var parameter to another routine. (But when you pass an object reference as a constant parameter, you can still modify the object's properties.)

Syntax Example:
function Add(a, b: integer) : integer; 
  Result := a + b;
procedure ReplaceTime(var pDT: TDateTime; const pNewDT: TDateTime);

Working Example

Assuming a button on a form, here's the supporting code for a quick demo:

unit ParametersUnit;
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, StdCtrls;
   TForm1 = class(TForm)
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
     { Private declarations }
     { Public declarations }
   Form1: TForm1;
{$R *.dfm}
function Add(a, b : integer) : integer;
   Result := a + b;
function AddByRef(var a, b : integer) : integer;
   Result := a + b;
function AddByConst(const a, b : integer) : integer;
  Result := a + b;
procedure TForm1.Button1Click(Sender: TObject);
  x, y: Integer;
   x := 4;
   y := 3;
   ShowMessage('by val pass literals 2+2=' + IntToStr(Add(2,2)));
   ShowMessage('by val pass vars 4+3=' + IntToStr(Add(x,y)));
   //With by reference, you cannot pass literals!
   //Does not work...ShowMessage('by ref 4+3=' + IntToStr(AddByRef(2,2));
   ShowMessage('by ref pass vars 4+3=' + IntToStr(AddByRef(x,y)));
  //With by constant, the compiler optimizes parameters.
   ShowMessage('by const pass literals 2+2=' + IntToStr(AddByConst(2,2)));
   ShowMessage('by const pass vars 4+3=' + IntToStr(AddByConst(x,y)));

Default Paremeters

Delphi introduced default parameters with Delphi 4. Default parameters is a form of overloading that allows you to specify a default for a parameter which is used when the routine is called without that particular parameter.

For example, the following function demonstrates default parameter overloading. It allows you to add up to four numbers:

function Add(a, b: integer; c:integer=0; d:integer=0): Integer;
  Result := a+b+c+d;

To test our new function, add the following code to a button click event:

ShowMessage('2+2=' + IntToStr(Add(2,2)));
ShowMessage('2+2+3=' + IntToStr(Add(2,2,3)));
ShowMessage('2+2+3+4=' + IntToStr(Add(2,2,3,4)));

More Info


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1207A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.


1 Beginner Level Question

Question #1: True or False?

The default for parameters for procedures, functions, and methods is by value.

  • True
  • False

  •  KB Article #101627 Counter
    Since 12/21/2008
       Contact Us!
    PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

    2,824 People Online Now!!  
    Sign In to see who's online now!  Not a member? Join now. It's free!
    Show more stats...

    ©1995-2019 PrestwoodBoards  [Security & Privacy]
    Professional IT Services: Coding | Websites | Computer Tech