冬雪雪冬 发表于 2017-8-15 16:47:03

浅谈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;



不多太少 发表于 2018-2-25 09:05:49

看不懂了

君非夜 发表于 2019-11-28 16:17:58

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.

13328593595 发表于 2023-8-20 09:29:14

懵懵懂懂的感觉
页: [1]
查看完整版本: 浅谈Delphi过程与函数03 - 零基础入门学习Delphi022