Lazarus

Programming => General => Topic started by: Goodman H__ on November 15, 2010, 12:00:20 am

Title: Does FP support user defined variadic procedure/functions
Post by: Goodman H__ on November 15, 2010, 12:00:20 am
Greetings!

As subjected,does fp support user defined variadic procedure or function like those provided in unit system,say writeln,readln?For example:

procedure printArray(ele:integer...);
begin
...
end.

test code:
printArray(1);//prints 1
printArray(1,2,3,4,5);//prints 1 2 3 4 5

Thanks in advance.
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Martin_fr on November 15, 2010, 12:56:38 am
the closest I can think of

procedure Foo(Data: Array of Integer);

Foo([1,2,3]);

note the []

http://www.freepascal.org/docs-html/ref/refsu59.html#x136-14600011.4.5

procedure Foo(Data: Array of const);

Foo([1,2,'abc', True]);

http://www.freepascal.org/docs-html/ref/refsu60.html#x137-14700011.4.6


Title: Re: Does FP support user defined variadic procedure/functions
Post by: Leledumbo on November 15, 2010, 01:23:41 am
Just FYI, Read(Ln)/Write(Ln) isn't a true function. It's actually a command for the compiler, which will map to internal functions based on the type of arguments.
Title: Re: Does FP support user defined variadic procedure/functions
Post by: marcov on November 15, 2010, 06:39:45 am
As subjected,does fp support user defined variadic procedure or function

yes

Quote
like those provided in unit system,say writeln,readln?For example:

no.

FPC provides two types of variadic functions, but the writeln/readln ones are not real procedures and behave differently.

The main variadic type is like this

Quote
procedure bla (xx:array of const);

and can only take basetypes as argument (so not e.g. records). See the manual for more details

the second type has the same (array of const) syntax, but when used in combination with cdecl, it is meant to
interface to C style variadic functions.

 
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Goodman H__ on March 12, 2012, 02:38:19 am
Just noticed there is a 'varargs' modifier in the fp2.6.1.Follwoing the sample code in the ref.chm,I tested in the IDE:
Code: [Select]
Function PrintF1(fmt : pchar); cdecl; varargs; 
                               external ’c’ name ’printf’;
PrintF1(’%d %d\n’,1,1);
Actually PrintF1 is a function other than a procedure,so maybe the proper on could be:
Code: [Select]
Function PrintF1(fmt : pchar):integer; cdecl; varargs; 
                               external ’c’ name ’printf’;

Here is the complete program:
Code: [Select]

program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };
Function PrintF1(fmt : pchar):integer; cdecl; varargs;
                               external 'c' name 'printf';
begin
  PrintF1('%d %d\n',1,1);
  readln;
end.
                 

It compiles,but when it tried to startup,System Error:
Code: [Select]
The program can't start because c.dll missing from your computer.Try reinstalling the program to fix the problem.
I don't think there is such a c.dll in the OS.(My OS is Win7).
So could anybody figure me out what the the problem here?

BTW.Can I omit the extern 'c ' name blabla and define my own variadic functions/procedures ?

Thanks for your help in advance.
Title: Re: Does FP support user defined variadic procedure/functions
Post by: User137 on March 12, 2012, 04:01:49 am
You have only a PrintF1 function header, but its missing its body. external 'c' is propably trying to tell that the function is found in c.dll file. Maybe you found a project in which someone made a dll with C, and then made header with pascal?
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Goodman H__ on March 12, 2012, 07:08:24 am
You have only a PrintF1 function header, but its missing its body. external 'c' is propably trying to tell that the function is found in c.dll file. Maybe you found a project in which someone made a dll with C, and then made header with pascal?

Thanks.But 'printf' isn't in the core C library?Why does one need to implement another version of printf?
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Leledumbo on March 12, 2012, 07:10:49 am
Quote
Thanks.But 'printf' isn't in the core C library?Why does one need to implement another version of printf?
Yes it is. It resides in libc.so in *nix and msvcrt.dll on windows, you what you need to change is the {$linklib} directive to {$linklib msvcrt} instead of {$linklib c} (or you don't specify external 'c', if you do change this to external 'msvcrt' instead)
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Goodman H__ on March 12, 2012, 07:59:39 am
Quote
Thanks.But 'printf' isn't in the core C library?Why does one need to implement another version of printf?
Yes it is. It resides in libc.so in *nix and msvcrt.dll on windows, you what you need to change is the {$linklib} directive to {$linklib msvcrt} instead of {$linklib c} (or you don't specify external 'c', if you do change this to external 'msvcrt' instead)

Thank you so much.change external 'c' to external 'msvcrt' fixed the issue and the app runs finally.

But...it seems the the escape sequence '\n' does not make any sense here.

Regards,
Sam
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Leledumbo on March 12, 2012, 04:45:49 pm
Quote
But...it seems the the escape sequence '\n' does not make any sense here.
Escape characters are handled by the C compiler, not printf function (otherwise, it won't be usable in other contexts, right?). For this, use Pascal equivalent (#10), but it's better to use LineEnding constant as it's already made cross platform.
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Goodman H__ on March 13, 2012, 01:51:32 am
Quote
But...it seems the the escape sequence '\n' does not make any sense here.
Escape characters are handled by the C compiler, not printf function (otherwise, it won't be usable in other contexts, right?). For this, use Pascal equivalent (#10), but it's better to use LineEnding constant as it's already made cross platform.

Got it,exactly.Thank you.
Title: Re: Does FP support user defined variadic procedure/functions
Post by: Laksen on March 13, 2012, 02:35:42 am
Do you need this specifically for the printf function? It seems a little excessive to require an entire C runtime library to run your program for some functionality that is already served by the nice Format function: http://www.freepascal.org/docs-html/rtl/sysutils/format.html
TinyPortal © 2005-2018