Freelance Writing Jobs | Today's Articles | Sign In

 

Procedure, subroutine or function?

Programming terminology 101 - a look at the differences in approach and definition of procedures, subroutines and functions.

Oct 25, 2006 Guy Lecky-Thompson

In which we examine a little bit of programming history, in an attempt to understand the differences between the three fundamental classes of user defined code blocks.

Introduction

Firstly, we need to examine two different programming paradigms:

  • Procedural programming
  • Object Oriented programming

The important difference between these two is that procedural programming treats every program as a list of instructions to be read in a top-down fashion, while Object Oriented (or OO) programming relies on the interaction of different objects to achieve the programs goal.

Each object, in OO terms, may be composed of procedural code, and some procedural programs are broken up into modules, giving a sense that they are, in some way, object oriented. Hence, the difference is not so much a programming issue as a paradigm.

Historically, the first programs were entirely procedural. Mechanisms were introduced to allow the interpreter of the programs to move around in the code (jump), but there was no sense of independence in the code blocks.

Languages such as C++ introduced mechanisms (such as the class) which allowed object orientation, but individual code segments were still procedural in nature. Hence this discussion is as pertinent now is it ever was, but the terminology is in a state of flux.

What is a Subroutine?

A subroutine is a piece of code executed upon demand, separate from the main flow of the program. The interpreter will jump to the code, execute it, and return to the main program.

Keywords such as GOSUB and RETURN, from the BASIC language, often used with line numbers, provided a programmer-friendly way to achieve this. The assembly language equivalents were less easy to digest, usually comprising a comparison, jump (or branch) and return, complete with offsets, memory addresses, or, in more modern assemblers, labels.

Subroutines are considered by many to be hard to maintain, difficult to read and digest, and are held in a similar light to the GOTO statement. In other words – reserved for use when nothing else will do.

It is worth noting that a modern language, implementing a modular structure, with named labels rather than line numbers, might be able to include subroutines in an acceptable fashion. However, with functions and procedures available, there may be no call for this approach at all.

Generically, the term subroutine can be used to denote a piece of code, separate from the main body, fulfilling a discrete task, in language-neutral terms. For example, a document might refer to the file handling subroutines.

What is a Procedure?

A procedure is a named block of code, like a subroutine, but with some additional features. For example, it can accept parameters, which might be input, output, or pass-through.

Traditionally, a procedure returning a value has been called a function (see below), however, many modern languages dispense with the term procedure altogether, preferring to use the term function for all named code blocks.

Subsequently, the keyword PROCEDURE exists only in certain languages, and has disappeared from many. It is worth noting that languages like C do not use it, and that BASIC based languages do, whereas Modula and Pascal based languages have both the PROCEDURE and FUNCTION keywords, in which a FUNCTION is a PROCEDURE that returns a value.

What is a Function?

As we saw above, a function is considered in some languages to be a procedure that returns a value. However, it is usually the term used to refer to any named code block. It is worth noting that C based languages use the function keyword exclusively.

Functions can accept parameters, return values, and are usually maintained separately from the main program code. Many programming languages have a special kind of function (in C, the main function) designated as the entry point to a program.

BASIC based languages have no such entry point, since they are entirely procedural in that execution begins at the top of the program, and continues until it is told to stop.

What is a Method?

Put simply, a method is a function contained within an object. In object oriented terms, we always speak of objects and their methods and properties. Each method is an action that we can ask the object to perform on one or more of their properties.

Conclusion

Choosing the correct terminology could be an issue when trying to put across language-neutral ideas. For example, a design specification that always uses the term method is pre-supposing an object oriented implementation, which may not be the goal of the document.

Subsequently, the tendency might be to use terms which are either no longer widely used in programming languages (such as subroutine), or invent language independent terms. It is usually a question of policy, and as such, different organisations will have different interpretations of what constitutes the correct term.

The copyright of the article Procedure, subroutine or function? in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Procedure, subroutine or function? in print or online must be granted by the author in writing.
Calls, SXC.hu Calls
   
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 7+2?

Comments

Aug 22, 2008 8:45 AM
Guest :
This articles is very good....everybody should raed it..
1 Comment:
;