浅谈Delphi过程与函数03 - 零基础入门学习Delphi022
本课讲函数,它有返回值,而过程没有。function 函数名(形参表):返回值类型;Var//声明常量、变量、过程、函数Begin//程序体End;返回值通过给函数名赋值或给result赋值来实现,看来最好在命名一般变量时最好别用result,以免混淆。函数体内至少要给其赋值一次,否则出错。函数的调用可以嵌套,过程也一样。习题是使用过程交换两个edit的内容。其实题目非常简单,主要程序如下:procedure TForm1.Button1Click(Sender: TObject);var
x, y:string;
begin
x:= edit1.Text;
y:= edit2.Text;
Swap_Fun(x, y);
edit1.Text:= x;
edit2.Text:= y;
end;
procedure TForm1.Swap_Fun(var str1, str2: string);
var
temp: string;
begin
temp:= str1;
str1:= str2;
str2:= temp;
end;
看不懂了 unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
function Swap_Fun(var str1, str2: string):String;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
x, y:string;
begin
x:= edit1.Text;
y:= edit2.Text;
Swap_Fun(x, y);
edit1.Text:= x;
edit2.Text:= y;
end;
function TForm1.Swap_Fun(var str1, str2: string):String;
var
temp: string;
begin
temp:= str1;
str1:= str2;
str2:= temp;
end;
end.
懵懵懂懂的感觉
页:
[1]