sub
sub

Reputation: 2733

Language construct naming: Function/Goto

How is a language construct with the following properties called?

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

Answers (4)

Mihai Stancu
Mihai Stancu

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:

  • it has isolated scope (so that it doesn't mess-up innocent by-standing variables);
  • it has an optional argument list with IN, OUT or INOUT parameters;
  • it does not 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

Steve Jessop
Steve Jessop

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

Ernelli
Ernelli

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

Josh Lee
Josh Lee

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

Related Questions