Reputation: 2733
How is a language construct with the following properties called?
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.
Upvotes: 2
Views: 146
Reputation: 16107
Since the question is tagged "language-agnostic" i'd add that construct of subroutines
is synonymous to the construct of procedures
.
There are some language dependent nuances to procedures
for example the SQL implementation:
IN
, OUT
or INOUT
parameters;return
anything, just alters the values of the OUT
or INOUT
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 a procedure
/subroutine
.
Upvotes: 0
Reputation: 279255
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.
Upvotes: 1
Reputation: 4040
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.
Upvotes: -1
Reputation: 177574
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.
Upvotes: 2