Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (4)


Variable number of argumentsGo to Sigurdur Hannesson's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
How to create functions that can accept n number of arguements.
Product:
Delphi all versions
Category:
Others
Skill Level:
Scoring:
Last Update:
07/14/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet functions arrays variable arguements
Times Scored:
4
Visits:
2768
Uploader: Sigurdur Hannesson
Company: Iceware
Reference: N/A
 
Question/Problem/Abstract:
In some situations, it can prove to be useful and convient to have a function that can accept X number of parameters.
For example:

At point A you'll write a function to add 3 integers together, and of course it'll only accept three parameters.
At point B, you'll need an identical function, but which can accept 10 integers.

So instead of doing the typical 1 + 2 + 3 + 4 etc. (this can very tedious to do, especially with more than let's say 15 numbers), you'll make a function that can accept as many (or as few) arguements as you'll ever need.

Following is an example of a function that accepts N number of arguements, adds the passed numbers together and returns the result.
Answer:



function AddNumbers( const ArgsList : array of const ) : Integer;
var
  ArgsListTyped: array[0..$FFF0 div SizeOf(TVarRec)] of TVarRec absolute ArgsList;
  n: Integer;
begin
  result := 0;
  for n := Low( ArgsList ) to High( ArgsList ) do
  begin
    with ArgsListTyped[ n ] do
    begin
      case VType of
        vtInteger: result := result + VInteger;
      end;
    end;
  end;
end;

// AddNumbers() will return the sum of all the integers passed to it
{
  AddNumbers( [1, 2, 3] ); will return 6
  AddNumbers( [4, 5, 6, 7, 8, 9, 10, 11] ); will return 60
}

{ Following code was suggested by Luis Ortega.
  Thanks Luis!
}

function AddNumbers( const ArgsList : array of Integer ) : Integer;
var
  n: Integer;
begin
  result := 0;
  for n := Low( ArgsList ) to High( ArgsList ) do
        result := result + ArgsList[n];
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Another way
    Yoav Abrahami (Jul 15 2002 10:22AM)

There is no need to delcate a private array in the function. you can use a code like this (Copied from Delphi Help file):

function MakeStr(const Args: array of const): string;

const
  BoolChars: array[Boolean] of Char = ('F', 'T');
var
  I: Integer;
begin
  Result := '';
  for I := 0 to High(Args) do
    with Args[I] do
      case VType of
        vtInteger:    Result := Result + IntToStr(VInteger);
        vtBoolean:    Result := Result + BoolChars[VBoolean];
        vtChar:       Result := Result + VChar;
        vtExtended:   Result := Result + FloatToStr(VExtended^);

        vtString:     Result := Result + VString^;
        vtPChar:      Result := Result + VPChar;
        vtObject:     Result := Result + VObject.ClassName;
        vtClass:      Result := Result + VClass.ClassName;
        vtAnsiString: Result := Result + string(VAnsiString);
        vtCurrency:   Result := Result + CurrToStr(VCurrency^);
        vtVariant:    Result := Result + string(VVariant^);
        vtInt64:      Result := Result + IntToStr(VInt64^);

    end;
end;
Respond

another example...
    Eber Irigoyen (Jul 14 2002 10:45PM)

here's another example of good use of this kind of parameters:
Goody for a statusbar

salu2
EberSys
Respond

performance...
    Luis Ortega (Jul 14 2002 2:26AM)

if all the processed parameters are integer then why not declare an  array of integers?

function AddNumbers( const ArgsList : array of Integer ) : Integer;
var
  n: Integer;
begin
  result := 0;
  for n := Low( ArgsList ) to High( ArgsList ) do
        result := result + ArgsList[n];
end;

Respond

RE: performance...
Sigurdur Hannesson (Jul 14 2002 2:53AM)

You're right. :)

I'm sorry, but somehow it never came to my mind.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Hans Gulö
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)