//要点15: 调用其他单元的函数
//包含函数的单元:
unit Unit2;

interface

function MyFun(x,y: Integer): Integer; {函数必须在接口区声明}

implementation

function MyFun(x,y: Integer): Integer; {函数必须在函数区实现}
begin
  Result := x + y;
end;

end.


//调用函数的单元:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2; {必须 uses 定义函数的单元}

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  i := MyFun(1,2);         {调用函数}
  //i := Unit2.MyFun(1,2); {有时为了避免重名, 需要这样调用}
  ShowMessage(IntToStr(i)); {3}
end;

end.

posted on 2008-01-14 21:34  万一  阅读(6792)  评论(0编辑  收藏  举报