Board index » delphi » Q. Procedure with OPTIONAL parameters

Q. Procedure with OPTIONAL parameters

I'm using TPW 1.5.
 

Re:Q. Procedure with OPTIONAL parameters


I'm using TPW 1.5.

Oops!  Sorry about that first posting.
I want to create a procedure with optional parameters.
For example...

Procedure DoSomething (A: Word; B: Boolean);
begin
end;

I want to be able to call the procedure like so:  DoSomething(5)
and also like so:  DoSomething(5, True)

The procedure would perform a different task depending on whether parameter B was
passed or not.

Is this possible in Pascal?  If so, can you tell me how?
Thanks in advance.

Re:Q. Procedure with OPTIONAL parameters


Quote
In <4gtk8b$...@c14dn2.pri.agt.ab.ca>, mjon...@ent.agt.ab.ca writes:
>I'm using TPW 1.5.

>Oops!  Sorry about that first posting.
>I want to create a procedure with optional parameters.
>For example...

>Procedure DoSomething (A: Word; B: Boolean);
>begin
>end;

>I want to be able to call the procedure like so:  DoSomething(5)
>and also like so:  DoSomething(5, True)

>The procedure would perform a different task depending on whether parameter B was
>passed or not.

>Is this possible in Pascal?  If so, can you tell me how?
>Thanks in advance.

This is not possible in Pascal per se.  You could pass a pointer to a
variable length list and the function/procedure could parse the
parameters, but there is no way to use C-like variable parameter lists
in Turbo Pascal.

--
Wayne D. Hoxsie Jr. <hoxs...@crl.com>
The Lab BBS, Alton IL, 618-462-0767
{*word*30} the ``Communications Decency Act!''
`finger' or `http://www.crl.com/~hoxsiew' for a PGP key.

Re:Q. Procedure with OPTIONAL parameters


Why not:-

const
  dso_SwitchOn   = 1;
  dso_SwitchOff  = 2;
  dso_SwitchNowt = 3;

procedure DoSomething (A: Word; B: Integer);
  begin
    {Some case logic to test 1, 2 or 3}
  end;

Then call:-

DoSomething(5,dso_SwitchNowt);

--
/----------------------------------------------------------\
| Kedgeree the Elder | These opinions are not necessarily  |
| @Milton Keynes     | mine let alone those of the company |
\----------------------------------------------------------/

Re:Q. Procedure with OPTIONAL parameters


Quote
>In <4gtk8b$...@c14dn2.pri.agt.ab.ca>, mjon...@ent.agt.ab.ca writes:
>>I want to create a procedure with optional parameters.
>>For example...

>>Procedure DoSomething (A: Word; B: Boolean);
>>begin
>>end;

>>I want to be able to call the procedure like so:  DoSomething(5)
>>and also like so:  DoSomething(5, True)

>>The procedure would perform a different task depending on whether parameter B was
>>passed or not.

>>Is this possible in Pascal?  If so, can you tell me how?

Not possible in Turbo/Borland Pascal - and there are as many different ways
to get around this limitation as there are pascal users.

The way I would start off is by writing two completely separate procedures,
then examine them for similarities that could be moved to a third procedure
that both of the first two use:

procedure DoMostOfTheStuff(A: Word);
begin
end;

proc DoSomeThingA(A: Word);
begin
   {Do stuff special for this proc.}
   DoMostOfTheStuff(A);
   {Do more stuff special for this proc.}
end;

proc DoSomeThingB(A: Word; B: Boolean);
begin
   {Do stuff special for this proc, perhaps using parameter B}
   DoMostOfTheStuff(A);
   {Do more stuff special for this proc.}
end;

You may even find that all you need is two procedures: one that handles
the cases where you don't need the "B" parameter, and one that calls
the first one to do most of the work and in addition does specialized
things with "B".

Other Threads