views:

59

answers:

3

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.

+2  A: 

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.

jleedev
Thanks, I took a closer look at BASIC's gosub now and it seems exactly like what I'm searching for here. It'll be a subroutine then, I suppose.
sub
@sub You might be interested in Python's and C#'s generators.
jleedev
A: 

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.

Ernelli
What? Isn't a whole program written in a scripting language a script?
sub
Sure, but a shell script is like a command that can be invoked by itself (recursion) or by another script (function), by prohibiting arguments it makes the construct hard to use in practice. Such a limitation will probably be circumvented by using global variables but that will lead to code that is very hard to maintain.
Ernelli
+1  A: 

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.

Steve Jessop