Recent

Author Topic: Variable number of parameters to a procedure  (Read 5119 times)

graemejc

  • New Member
  • *
  • Posts: 33
Variable number of parameters to a procedure
« on: May 23, 2018, 02:33:00 pm »
In the same way that predefined procedures such as writeln, I want to be able to write a procedure with a variable number of parameters ... I've done some searching and only found references that a variable number of arguments can be used but can't find out how. Apologies if this is a dumb question. I've been out of programming since 1988.

procedure menu (mo1,mo2,...mof:String; var menuoption:word);
{ mo=menu option. }
begin
           gotoxy(x,y      ); write(mo1);
           gotoxy(x,y+1  ); write(mo2);
           ...
           gotoxy(x,y+f-1); write(mo1);
           readln(menuoption);
end;

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Variable number of parameters to a procedure
« Reply #1 on: May 23, 2018, 02:39:07 pm »
Declare your procedure as:
procedure menu(moX: array of string; var menuoption: word);

Then call:

menu( ['aaa', 'bbb', ...], 123);

You can use length(moX) inside the procedure to detrect how many parameters was send.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

graemejc

  • New Member
  • *
  • Posts: 33
Re: Variable number of parameters to a procedure
« Reply #2 on: May 23, 2018, 03:13:49 pm »
Thanks. Very much appreciated.

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Variable number of parameters to a procedure
« Reply #3 on: May 24, 2018, 08:12:07 pm »
You can use length(moX) inside the procedure to detect how many parameters was send.
I am not sure if Length will always work well in this case. At least I remeber cases when it did not. It is safer to include one more parameter: NParams:integer and pass actual number of elements in the array in this variable.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: Variable number of parameters to a procedure
« Reply #4 on: May 24, 2018, 09:17:43 pm »
I am not sure if Length will always work well in this case.

Yes, it will.

At least I remeber cases when it did not.

Can you please show an example of such a case?

It is safer to include one more parameter: NParams:integer and pass actual number of elements in the array in this variable.

An Open Array parameter already does exactly that behind the scenes.  An Open Array is actually passed as 2 parameters - a pointer to the 1st element, and the high index of the array.  Calling Length() on an Open Array returns the high parameter + 1.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Variable number of parameters to a procedure
« Reply #5 on: May 25, 2018, 12:51:02 am »
Hmm, looks like my information is obsolete  :-[

I tested it right now and indeed, both length() and High() functions worked correctly, where Length() = High() + 1.
I remember, is some older versions it did not really work when actual parameters were dynamic arrays.

Maybe, my memory stems from Turbo Pascal which did not have SetLength. After MemAlloc it could not work of course...

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Variable number of parameters to a procedure
« Reply #6 on: May 25, 2018, 02:10:44 am »
Length reports the total element count:

High reports the highest index value you can use...

If an array starts at 1 instead of 0, then HIGH will report the same as Length.

so the LOW and HIGH reports the lowest and highest index value you can use and the
length is just the total number of elements.

so be careful

 Dynamic arrays are normally 0 based, this means the HIGH will report one less than the length but it
gives you the limit of your INDEX..

If you had an array of [10..20] this becomes very important.
 
because HIGH = 20 where as Length = 11
The only true wisdom is knowing you know nothing

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Variable number of parameters to a procedure
« Reply #7 on: May 25, 2018, 10:07:53 am »
Well, open array in a procedure is always 0-based, regardless of passed actual parameter, and always High() = Length()-1.

Consider following program:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.   MyArr:array[5..10] of integer;
  4.   procedure GetArr(AnArr: array of Integer);
  5.   begin
  6.     writeln(length(AnArr));
  7.     writeln(High(AnArr));
  8.   end;
  9. begin
  10.   GetArr(MyArr);
  11.   readln;
  12. end.
  13.  

It will print:
6
5

even though High(MyArr) is 10.

andyH

  • Jr. Member
  • **
  • Posts: 99
Re: Variable number of parameters to a procedure
« Reply #8 on: January 04, 2020, 06:58:24 pm »
Bit late to the party on this one, was searching for similar.

If you know how many and what the type of parameters are in advance, but they just are not relevant to the current circumstance you can declare a procedure as:

Code: Pascal  [Select][+][-]
  1. procedure myproc(var1 : integer; var2 : integer = 0; var3 : string = ''; and so on...);

then in the procedure test that var2 = 0 or var3 = '' and take appropriate action.

Valid calls to the procedure would be myproc(23) or myproc(15,101) or myproc(1,1,'hello')


MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Variable number of parameters to a procedure
« Reply #9 on: January 04, 2020, 07:34:18 pm »
Bit late to the party on this one, was searching for similar.

If you know how many and what the type of parameters are in advance, but they just are not relevant to the current circumstance you can declare a procedure as:

Code: Pascal  [Select][+][-]
  1. procedure myproc(var1 : integer; var2 : integer = 0; var3 : string = ''; and so on...);

then in the procedure test that var2 = 0 or var3 = '' and take appropriate action.

Valid calls to the procedure would be myproc(23) or myproc(15,101) or myproc(1,1,'hello')

If nothing else, that implies that the string will always be passed even if it's not needed, which will probably have a performace implication (possibly a bit less if it was passed as const).

I suppose that an interesting question is the point at which passing a sequence of integers (defaulting to -1 or whatever if not used) becomes more expensive than passing an open array of integers.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Variable number of parameters to a procedure
« Reply #10 on: January 04, 2020, 10:07:41 pm »
For that kind of situation (variable number of parameters of differing types) is for what array of const was introduced.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018