Thursday, April 25, 2013

Functions, Procedures, Predicates, and Methods

Many programming language have what they call functions. These are pieces of code with formal parameters that you can call with arguments from different places in the code. Functions can return a value but in many languages it is also possible for a function to return no value. It can be called just to generate side effects.

In some languages such as Pascal they distinguish between functions and procedures. Functions return values and procedures do not.

Other languages have neither functions nor procedures but do have methods. Methods are like functions attached to an object. You call a method on an object as a way of sending message to the object.

Still other languages such a Prolog have none of the above but do have predicates. A predicate does not return a value or have a traditional side-effect. What is does is impose constraints on its arguments.

What these things all have in common is that they are ways to encapsulate a bit of computation or evaluation. Sometimes it is useful to distinguish between them, but in a lot of cases, I want to make a point about all of those things at once or about several of them at once, and it just gets confusing trying to say what I am talking about. In the past I've used the word "operation" to refer to all of those things at once, but that seems a bit awkward and can be confused with "operator".

Which reminds me, some language have a fifth kind of encapsulated computation that they call operators. In C++ operators are just functions that use operator syntax instead of function syntax.

From here on I'm going to try to use "routine" to refer to all of these things for encapsulating computation --the five I've mentioned and any others that come up. Sometimes it may not include predicates which are a bit different from other kinds of routines, but I'll try to make it clear in context. "Routine" is short, convenient, and ambiguous, which is what I'm looking for. To purists, it will imply an imperative style of programming, which isn't what I intend, but unless someone can come up with something better, that's what I'm going with.

No comments:

Post a Comment