delphi分为过程和函数:
delphi 过程以保留字procedure开始,没有返回值;函数以保留字function开始,有返回值。
一: 过程的框架:
Procedure 过程名([形参列表])// 参数可选
var
//声明常量、变量或另一个过程或函数等
begin
语句;
end;
界面如图所示:
运行前:
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Myproc(Str: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
x: String;
begin
x:= Edit1.Text;
Myproc(x);
end;
procedure TForm1.Myproc(Str: String);
begin
ShowMessage(Str)//就是弹出一个窗口
end;
end.
运行后:
二:函数的框架:
Function 函数名(形参表): 返回值类型;
局部声明
begin
语句;
end;
function 实例:
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
function add(num1 :Integer;num2 :Integer) :Integer;//定义加法
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
add(1,2);
end;
function TForm1.add(num1 :Integer;num2 :Integer) :Integer;//函数的具体描述
var
sum:Integer;
begin
sum:=num1+num2;
Edit1.Text:=inttostr(sum);
end;
end.
运行结果: