Recent

Author Topic: Writing procedures/functions with infinite number of parameters  (Read 4609 times)

Rave

  • Full Member
  • ***
  • Posts: 165
I always wanted to know how to write these and no resource on web was helpful for me. Example of such functions are Write/Read. They can take any number of parameters.

Now, let's say that I want to write Max function that would return maximum number of numbers provided via parameters. Parameters can be anywhere from 1 to infinity. How to write such function.

Note that arrays solutions are no go here, because I want to learn how to write functions that can take any number of parameters specifically

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Writing procedures/functions with infinite number of parameters
« Reply #1 on: April 26, 2013, 01:24:23 pm »
write/read functions are compiler magic functions and their behavior can not be duplicated that said there are a couple of ways to accomplish the task of unknown parameters the most useful one is the open array construct which you can use on your Max example eg
Code: [Select]
function Max(const Items : array of integer):integer;
var
  vCntr : integer;
begin
   Result := Items[Low(Items);
   For vCntr := low(Items)+1 to High(Items) do
     if Result < items[vCntr] then result := Items[vCntr];
end;
you call it like
Code: [Select]
vMax := Max([1,5,15,width,height,4096,12]);
an other way is
Code: [Select]
function Max(Items : array of const) : integer;
var
  vCntr : integer;
begin
  result := 0;
  for vCntr := low(Items) to high(Items) do
  if (Items[vCntr].VType = vtInteger) and (Items[vCntr].VInteger > Result) then Result := Items[vCntr].VInteger;
end;
and you call it
Code: [Select]
  vMax := Max(1,'my name', 5,4096, width, height, left, top, now);

array of const is a special case of open array that it can be thought to be equal to
function Max(Items : array of TVarRec):integer;

of course that has nothing to do with the C equivalent of open parameter list and it most likely is incompatible with such a construct.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Writing procedures/functions with infinite number of parameters
« Reply #2 on: April 27, 2013, 03:08:11 am »
Write and Read (and some other RTL functions) are not true functions. They are more like "command" to the compiler to generate correct functions. If you dump the generated assembly, you'll see. e.g.:
Pascal:
Code: Pascal  [Select][+][-]
  1. var
  2.   x: Integer;
  3. begin
  4.   Write('Enter an integer: ');
  5.   ReadLn(x);
  6.   WriteLn('You entered: ',x);
  7. end.
  8.  
ASM (relevant parts only):
Code: ASM  [Select][+][-]
  1. # [4] Write('Enter an integer: ');
  2.         call    fpc_get_output
  3.         movl    %eax,%ebx
  4.         movl    $_$PROGRAM$_Ld1,%ecx
  5.         movl    %ebx,%edx
  6.         movl    $0,%eax
  7.         call    fpc_write_text_shortstr
  8.         call    FPC_IOCHECK
  9.         movl    %ebx,%eax
  10.         call    fpc_write_end
  11.         call    FPC_IOCHECK
  12. # [5] ReadLn(x);
  13.         call    fpc_get_input
  14.         movl    %eax,%ebx
  15.         leal    -4(%ebp),%edx
  16.         movl    %ebx,%eax
  17.         call    fpc_read_text_sint
  18.         call    FPC_IOCHECK
  19.         movw    -4(%ebp),%ax
  20.         movw    %ax,U_$P$PROGRAM_$$_X
  21.         movl    %ebx,%eax
  22.         call    fpc_readln_end
  23.         call    FPC_IOCHECK
  24. # [6] WriteLn('You entered: ',x);
  25.         call    fpc_get_output
  26.         movl    %eax,%ebx
  27.         movl    $_$PROGRAM$_Ld2,%ecx
  28.         movl    %ebx,%edx
  29.         movl    $0,%eax
  30.         call    fpc_write_text_shortstr
  31.         call    FPC_IOCHECK
  32.         movswl  U_$P$PROGRAM_$$_X,%ecx
  33.         movl    %ebx,%edx
  34.         movl    $0,%eax
  35.         call    fpc_write_text_sint
  36.         call    FPC_IOCHECK
  37.         movl    %ebx,%eax
  38.         call    fpc_writeln_end
  39.         call    FPC_IOCHECK
  40.  
Those fpc_read* and fpc_write* are the ones that really get the job done.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: Writing procedures/functions with infinite number of parameters
« Reply #3 on: April 27, 2013, 12:43:41 pm »
(array of const)
of course that has nothing to do with the C equivalent of open parameter list and it most likely is incompatible with such a construct.

Careful there. Array of const in cdecl functions IS FPC's original way of marking C open parameters list. Delphi introduced "VARARGS" later.

zzzato

  • New Member
  • *
  • Posts: 22
  • A delphi developer who is moving to debian
Re: Writing procedures/functions with infinite number of parameters
« Reply #4 on: May 09, 2013, 09:11:56 am »
I always wanted to know how to write these
cally[/i]

Why you need it?

A.
"Anything that can go wrong will go wrong"

 

TinyPortal © 2005-2018