语言结构命名:Function/Goto
具有以下属性的语言结构如何被称为?
- 它有一个开始和一个结束,就像一个函数
- 它有一个包含它的名称的头,也像一个函数,但没有参数
在它的开始和结束之间可以有任意数量的语句,就像一个函数
您可以使用函数从任何地方(甚至是它自己)跳转到它的开头,它将执行其中包含的语句,直到到达结尾< /p>
你可以使用一个函数立即停止其内容的执行,并且跳回到调用它的位置
它包含的代码与其他所有代码处于相同的范围内,因此您可以访问外部的所有变量并创建新的变量,这些变量在离开构造时不会被删除。
总而言之,它就像一个带有结束点的 goto
点,并且可以选择返回调用它的位置。
How is a language construct with the following properties called?
- It has a beginning and an end, just like a function
- It has a header containing it's name, also like a function but without arguments
There can be any number of statements between its beginning and end, like a function
You can use a function to jump to its beginning from anywhere (even itself) and it will execute the statements contained in it until it reaches its end
You can use a function to immediately stop the execution of its contents and jump back where it was called from
The code it contains is in the same scope as everything else, so you can access all variables outside and create new ones which aren't deleted upon leaving the construct.
All in all it is like a goto
point with an end and the option to return where it was called from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BASIC 有这个,它被称为 gosub ,它相对于正确函数的唯一优势是你的最后一点,其中所有变量都在同一范围内。除此之外,它很糟糕。
在面向对象的语言中,通过将所需的变量封装到一个对象中并让不同的方法相互调用,通常可以达到相同的效果。多个入口点不是大多数语言的功能,但您可以通过将方法分成更小的部分来解决这个问题。
BASIC had this, it was called
gosub
and its only advantage over a proper function was your last point, where all of the variables were in the same scope. Beyond that it sucked.In an object-oriented language, you could achieve generally the same effect by encapsulating the variables you want into an object and having different methods call each other. Multiple entry points are not a feature of most languages, but you can get around that by splitting your methods into smaller pieces.
闭包的概念可能是相关的。
闭包是一个函数,但它是在某个范围内定义的(比如说另一个函数),并且它可以访问该范围内的所有变量。因此,它具有您列出的大部分属性,除了在标头中声明和(通常)具有名称之外。无论如何,标头都是语言实现细节,而不是这样的功能:-)。通常,闭包可以从定义它们的函数中返回,并且在 GC 语言中,它们将维护对其使用的局部变量的引用。
还要考虑 Perl 有两种不同类型的作用域变量 - 词法变量(“my”)和动态变量(“local”)。词法变量是您在 C、Java 等语言中习惯使用的局部变量。动态变量对于从声明它们的块调用的任何函数都是可见的。因此,如果所有变量都使用
local
声明,则所有 Perl 函数都具有所需的属性。在所有情况下,我都错过了“创建在离开函数时不会被破坏的新变量”。这种情况非常罕见,因为它假设函数中声明的变量具有全局作用域,而这在大多数语言中并不是典型情况。通常,您可以通过拥有一些全局对象并在其上挂上一些东西来伪造它,但它很少有用。
The concept of a closure may be relevant.
A closure is a function, but it's defined in some scope (another function, let's say), and it has access to all variables in that scope. So it has most of the properties you list except for being declared in a header and (usually) having a name. Headers are in any case a language implementation detail rather than a feature as such :-). Usually closures can be returned out of the function in which they're defined, and in a GC language they will maintain references to the local variables they use.
Also consider that Perl has two different kinds of scoped variables - lexical variables ("my") and dynamic variables ("local"). Lexical variables are the locals you're used to from C, Java and so on. Dynamic variables are visible from any function called from the block which declared them. So if all your variables are declared with
local
, all Perl functions have the desired properties.In all cases, I missed "create new variables which aren't destroyed on leaving the function". That's pretty rare, since it sort of assumes that variables declared in functions have global scope, and that's not the typical case in most languages. You can normally fake it by having some global object and hanging stuff off that, but it's rarely useful.
由于问题被标记为“与语言无关”,我要补充一点,子例程的构造与过程的构造同义。
过程
有一些与语言相关的细微差别,例如 SQL 实现:IN
、OUT
或INOUT
参数;返回
任何内容,只是更改OUT
或INOUT
参数的值;@Steve Jessop
闭包
在我的书中是一种非常不同的怪物。虽然它们确实模仿了过程的许多功能,但它们有自己的参数列表和调用堆栈,这使得它们可以访问外部作用域而不是过程/过程子例程。
Since the question is tagged "language-agnostic" i'd add that construct of
subroutines
is synonymous to the construct ofprocedures
.There are some language dependent nuances to
procedures
for example the SQL implementation:IN
,OUT
orINOUT
parameters;return
anything, just alters the values of theOUT
orINOUT
parameters;@Steve Jessop
Closures
are a very different kind of monster in my book.While they do mimic many features of a
procedure
, they have their own argument list and call stack which makes them functions with access to external scope rather than aprocedure
/subroutine
.从程序的结构来看,我将其称为脚本。例如 shell/批处理脚本。
也许任务是这种结构的更好名称,它可以是使用 JavaScript 或 Perl 的脚本,只需引用脚本本身就可以按原样执行。
From the structure of the program, I would call it a script. E.g. a shell/batch script.
Maybe task is a better name for that kind of structure, it can be a script using JavaScript or Perl that can be executed as is by just referring to the script itself.