读书人

那种编译器实现了pascal的函数参数和过

发布时间: 2013-01-25 15:55:29 作者: rapoo

那种编译器实现了pascal的函数参数和过程参数功能?
Turbo pascal、Free pascal和delphi7都没有实现,以用就说function非法。
[解决办法]


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
private
{ Private declarations }
function xyz(x:integer):boolean;//定义一个类的"私有"函数、整型参数、返回布尔型值
public
{ Public declarations }
function abc(x:integer):boolean;//定义一个类的"公开"函数、整型参数、返回布尔型值
end;

var
Form1: TForm1;
function cdef(y:string):integer;//定义一个单元的"公开"函数、字符串参数、返回整型值

implementation

//函数 abc、cdef 可被外部单元引用,函数 xyz、hijk不能被外部单元引用。
//单元或类的过程,除无返回外,定义和编写过程体,与本例函数方法相同。
//单元或类的变量定义,摆放位置与本例函数定义的摆放位置同义。

{$R *.dfm}

function hijk(z:boolean):string;//省略定义的函数体(此为单元内私有函数),必须放在引用该函数的语句之前
begin
Result := 'False';
if z then Result := 'True';
end;

function TForm1.xyz(x: integer): boolean;//类的函数体,需在函数名前有类名作前导
begin
Result := false;
//......
end;

function TForm1.abc(x: integer): boolean;//类的函数体,需在函数名前有类名作前导
begin
Result := false;
//......
end;

function cdef(y:string):integer;//单元的函数,函数名前不需类名作前导
begin
Result := 0;
showmessage(hijk(true));//此处引用了一个省略定义的函数(该类函数或过程,须摆放在此之前!)
//......
end;

end.

[解决办法]
Delphi限制函数/过程的参数列表中不能定义类型,非基本类型的参数必须先在外部定义。
对于楼主的需求,只要先定义函数指针参数—elphi中称之为Procedural Types),然后将参数声明为此类型即可。

[解决办法]
不是都告诉你了吗,Delphi限制函数/过程的参数列表中不能定义类型,要在外部定义。

type
TFX = function(t:real):real;

然后:
function multiFun(fx: TFX; r:real):real;
就可以了。

[解决办法]
你不能像C/C++这样,直接在delphi的函数参数中声明函数指针类型:
void myCallback(void (*pro)(int ),int a)
{
xxx
}
delphi/pascal中应该这样用:

Type
Tpro=procedure(i:Integer);

procedure myCallback(pro:Tpro;a:Integer);
begin
xxxx
end;

读书人网 >.NET

热点推荐