Organize code into a procedure

Having trouble getting started? Check here...
Post Reply
L505
Posts: 7
Joined: Sun Oct 26, 2008 9:27 pm

Organize code into a procedure

Post by L505 »

Hello, is there a way to organize the code into something I can call? Such as a procedure, routine, or a function. For example one can enclose an operator but one cannot call this operator as if it were a procedure as far as I have studied and tried.

Also, does the Third Manifesto specify any information about organizing code into procedures, functions, etc.
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Organize code into a procedure

Post by Dave »

Tutorial D (and, hence, Rel) define OPERATORs, which behave like procedures and functions in other imperative programming languages.

For example, one could define a simple procedure as follows:

Code: Select all

OPERATOR blah (x INTEGER, y INTEGER);
  BEGIN;
    WRITELN "blah";
    WRITELN x + y;
  END;
END OPERATOR;
An operator like the above is invoked as:

Code: Select all

CALL blah(3, 4);
Or a simple function can be defined as follows:

Code: Select all

OPERATOR plus (x INTEGER, y INTEGER) RETURNS INTEGER;
  RETURN x + y;
END OPERATOR;
The above is invoked in an intuitive fashion, e.g.:

Code: Select all

VAR x INTEGER;
x := plus(5, 6);
OPERATORs may be nested, as in Pascal.

OPERATORs defined in the global scope are stored in the database, and must be explicitly dropped to eliminate them. E.g.:

Code: Select all

DROP OPERATOR blah(INTEGER, INTEGER);
DROP OPERATOR plus(INTEGER, INTEGER);
L505
Posts: 7
Joined: Sun Oct 26, 2008 9:27 pm

Re: Organize code into a procedure

Post by L505 »

I love the feeling of n00b in the beginning stages of exploring a new language. I feel this great Aha moment now - as so far I have been using Rel just like a scripting tool without putting ANY of my algorithms in procedures. Now I can start programming seriously, finally. I wondered why your Bottles Of Beer example on the internet did not have some procedures in it somewhere, too.

This feels similar to when I was learning OOP, and wondered why the so called "METHODS" were being discussed in literature, and not "PROCEDURES"? What was the difference, I wondered. In fact Wirth has an article on that subject, questioning why society continues to change terminology to sell old ideas as something new. In Tutorial Dee's case, though, I do not think it is just about selling an old idea as something new - I suspect there is more to it, such as OPERATORS being able to do more than just PROCEDE.. i.e. they can also OPERATE. TUPLES are very much like RECORDS also, but again I suspect the term TUPLE was chosen for a good reason : one which may be to rid us of our old STRUCT/RECORD habits and non-mathematical thinking.

What we know as PROCEDURES are also METHODS, FUNCTIONS (in Cee), OPERATORS, and SUBROUTINES (or even just plain ROUTINES). There are so many words that mean the same thing in programming paradigms, and it adds confusion (possibly it is partly English's fault). Classes are confused with objects, types are confused with classes, objects are confused with types, etc. etc. But anyway! Thanks for helping a n00b.
Post Reply