两个函数两种写法,有什么区别?

fj218 2003-09-17 12:15:56
第一种写法:
procedure AAA;

procedure BBB;
var
....
begin
....
end;

var
....
begin
....
end;

第二种写法:
procedure BBB;
var
....
begin
....
end;

procedure AAA;
var
....
begin
....
end;
我想应该是第一种写法BBB函数只能在AAA函数内部调用,而第二种写法BBB函数还能被AAA函数以外的函数调用。我还想知道其它更深层次的区别,比如那种写法效率高,更加优化等等。
...全文
43 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
很土 2003-09-17
  • 打赏
  • 举报
回复
这涉及函数的作用域问题, 第一种可以理解为 BBB 函数为 AAA 函数的私有函数; 而第二种可以理解为 BBB 和 AAA 同属于单元类中的一个方法, 相互可见, 若 BBB 要调用 AAA 则 AAA 函数必须先 forward 或者放在 interface 节中声明.
FrameSniper 2003-09-17
  • 打赏
  • 举报
回复
呵呵,这个问题问的好,俺还真是没有注意过!首先楼主的想法完全正确

至于谁效率高,更加优化!个人认为没有多大区别,几乎可以看成是一样的!
underwolf 2003-09-17
  • 打赏
  • 举报
回复
学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习
submarine81 2003-09-17
  • 打赏
  • 举报
回复
第一种写法能提高程序的健壮性,至于性能应该区别不大。 我要是写代码,不会在这里做
性能优化的 。 :)
popmailzjw 2003-09-17
  • 打赏
  • 举报
回复
我覺得是第一種效率高些﹐同時第一種方法也節約內存一些
IORILI 2003-09-17
  • 打赏
  • 举报
回复
关注
zhxfzhxf1 2003-09-17
  • 打赏
  • 举报
回复
同意 xiong1979(啃) \liqxdt(黑客剑)
cmain83(龙行天下2008)

"又当函数据不调用时,所占资源会立即被释放掉了,所以第一种的效率高。"
"第一种方式只能在其函数内部执行,外边的函数无法调用"
"BBB过程应该是一个临时定义过程,不占用系统固定内存吧"
等说法没有依据
1当函数调用结束时,第二种一样会释放资源
2仅是二者的作用域不同
3函数中的局部变量从来不占用系统的全局变量内存(?该名称是否确切),而是在栈上分配的

可以讨论的是cmain83(龙行天下2008) 所说的
如果类似于C的宏\C++的内联函数的话,会是第一种效率高
但PASCAL中我还未听说过内联函数的概念,不知是否有其事



FrameSniper 2003-09-17
  • 打赏
  • 举报
回复
窗体上放一个按钮,单元代码如下:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

procedure ProcA;
procedure ProcC;

var
Form1: TForm1;

implementation

procedure ProcC;
begin
ShowMessage('C : '+IntToStr(Integer(@ProcC)));
end;

procedure ProcA;
procedure ProcB;
begin
ShowMessage('B : '+IntToStr(Integer(@ProcB)));
end;
begin
ShowMessage('A : '+IntToStr(Integer(@ProcA)));
ProcB;
ProcC;
//ShowMessage('Form1 : '+IntToStr(Integer(@Form1)));
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
ProcA;
end;

end.


各位可以看看ProcA、B和C之间地址的关系!

另外两中方式对于栈(假设有参数和局部变量)的使用应该是没有任何区别的!
FrameSniper 2003-09-17
  • 打赏
  • 举报
回复
看看帮助中关于本地申明(Local Declarations)的内容!

The body of a function or procedure often begins with declarations of local variables used in the routine's statement block. These declarations can also include constants, types, and other routines. The scope of a local identifier is limited to the routine where it is declared.

Nested routines

Functions and procedures sometimes contain other functions and procedures within the local-declarations section of their blocks. For example, the following declaration of a procedure called DoSomething contains a nested procedure.

procedure DoSomething(S: string);
var
X, Y: Integer;
procedure NestedProc(S: string);
begin
...
end;
begin
...
NestedProc(S);
...
end;

The scope of a nested routine is limited to the procedure or function in which it is declared. In our example, NestedProc can be called only within DoSomething.

For real examples of nested routines, look at the DateTimeToString procedure, the ScanDate function, and other routines in the SysUtils
unit.
cmain83 2003-09-17
  • 打赏
  • 举报
回复
对了.

它相当于 C 的内联(inline)函数
cmain83 2003-09-17
  • 打赏
  • 举报
回复
第一种的效率高

因为 Delphi 在编译的时候, 把 B 函数的所有代码全部复制到调用 函数 B 的地方.
也就是说, 在程序执行的时候, 就不要进行函数的调用了.
这样, 执行较率也就高

而第二种方法
程序在执行的时候还要进行函数的调用.

这个叫什么 "XX联编" 的
我也记不太清了
libra163 2003-09-17
  • 打赏
  • 举报
回复
效率是一样的,
习惯问题。
whqcfp 2003-09-17
  • 打赏
  • 举报
回复
第一种效率高,
因为定义在一个函数的内部,
即保证了私有性,
又当函数据不调用时,
所占资源会立即被释放掉了,
所以第一种的效率高。
qiume 2003-09-17
  • 打赏
  • 举报
回复
学习!!!!!!!!!!!!!!
superyys 2003-09-17
  • 打赏
  • 举报
回复
此问题甚有创意!
dext 2003-09-17
  • 打赏
  • 举报
回复
两者效率是一样的
就合C语言一样
唯一不同的是申明的位置,也就是作用域不同
S海鸥 2003-09-17
  • 打赏
  • 举报
回复
效率上还没有什么差别,它们的差在调用范围.这是主要的.
很土 2003-09-17
  • 打赏
  • 举报
回复
非也,两者在调用上没多大区别,只是指令码稍有区别。

第一种时一定是 Call near 指令码为 $E8,使用的是相对地址;
而第二种有时是 Call near 而有时是 Call far,指令码为 $FF 或 $9A,使用的是绝对地址。

关于指令码可以参考《Intel Hex Opcodes And Mnemonics》,里面有不同情况指令码有区别。

当然在调用速度上也会有差别,只是差别不会很大。
第一种在指令运行上略快一些,基本上可以忽略不计,若如下情况时就有区别了:
for i := 1 to 1000000 do
BBB(...);
在这种情况下(即调用比较频繁时),最好使用第一种方式。
xah_JD 2003-09-17
  • 打赏
  • 举报
回复
在效率上还没有什么差别,它们的差在调用范围.这是主要的.
itperson 2003-09-17
  • 打赏
  • 举报
回复
第一种方式运行比较快
但第一种方式只能在其函数内部执行,外边的函数无法调用
加载更多回复(3)

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧