* * *

Author Topic: Does FP support user defined variadic procedure/functions  (Read 4712 times)

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Does FP support user defined variadic procedure/functions
« 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.
« Last Edit: November 15, 2010, 12:03:42 am by Goodman H__ »
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 2937
    • wiki
Re: Does FP support user defined variadic procedure/functions
« Reply #1 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


« Last Edit: November 15, 2010, 12:59:33 am by Martin_fr »

Leledumbo

  • Hero Member
  • *****
  • Posts: 5573
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Does FP support user defined variadic procedure/functions
« Reply #2 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.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2776
Re: Does FP support user defined variadic procedure/functions
« Reply #3 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.

 

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Does FP support user defined variadic procedure/functions
« Reply #4 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.
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

User137

  • Hero Member
  • *****
  • Posts: 1351
    • Nxpascal home
Re: Does FP support user defined variadic procedure/functions
« Reply #5 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?
Win7, FPC and Lazarus 32-bit SVN version.

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Does FP support user defined variadic procedure/functions
« Reply #6 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?
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

Leledumbo

  • Hero Member
  • *****
  • Posts: 5573
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Does FP support user defined variadic procedure/functions
« Reply #7 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)

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Does FP support user defined variadic procedure/functions
« Reply #8 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
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

Leledumbo

  • Hero Member
  • *****
  • Posts: 5573
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Does FP support user defined variadic procedure/functions
« Reply #9 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.

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Does FP support user defined variadic procedure/functions
« Reply #10 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.
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

Laksen

  • Sr. Member
  • ****
  • Posts: 458
    • J-Software
Re: Does FP support user defined variadic procedure/functions
« Reply #11 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

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads