music
OSdata.com: programming text book 

OSdata.com

subprograms

summary

    This subchapter looks at subprograms and subroutines.

free computer programming text book project

table of contents
If you like the idea of this project,
then please donate some money.
more information on donating

Google

stub section

    This subchapter is a stub section. It will be filled in with instructional material later. For now it serves the purpose of a place holder for the order of instruction.

    Professors are invited to give feedback on both the proposed contents and the propsed order of this text book. Send commentary to Milo, PO Box 1361, Tustin, California, 92781, USA.

subprograms and subroutines

    This subchapter looks at subprograms, subroutines, procedures, and functions.

    Alan Turing’s famous 1936 paper “On Computable Numbers, with an Application to the Entscheidungsproblem”, in which he described the Turing Machine, first introduced the idea of m-functions (also called m-m configuration functions or skeleton tables).

    Alan Turing’s m-functions are equivalent to and the predecesors of modern functions, procedures, suboutines, and methods.

valid function identifiers

    Quick summary of the rules for building valid function identifiers in several major languages, using regular expressions:

Ada[a-zA-Z](_?[a-zA-Z0-9])*
ALGOL-68[a-z][a-z0-9 ]*
Awk[_a-zA-Z][_a-zA-Z0-9]*
B[_a-zA-Z][_a-zA-Z0-9]*
BourneShell[_a-zA-Z0-9]+
C[_a-zA-Z][_a-zA-Z0-9]*
C#[_a-zA-Z][_a-zA-Z0-9]*
C++[_a-zA-Z][_a-zA-Z0-9]*
COBOL[a-zA-Z][a-zA-Z0-9-]*
30 character maximum
Classic REXX[a-zA-Z!?@#][a-zA-Z0-9!?@#]*
Common Lispanything without a space and is not a number
E[_a-zA-Z][_a-zA-Z0-9]*
Eiffel[a-zA-Z][_a-zA-Z0-9]*
F#[_a-zA-Z][_a-zA-Z0-9']*
FORTRAN[A-Z][A-Z0-9]*
maximum of six characters
Forthanything without a space and is not a number
GNU-bc[a-z][a-z0-9_]*
Haskell[_a-z][_a-zA-Z0-9']*
Java[_a-zA-Z$][_a-zA-Z0-9$]*
JavaScript[_a-zA-Z$][_a-zA-Z0-9$]*
Lispanything without a space and is not a number
Maple[_a-zA-Z][_a-zA-Z0-9]*
Mathematica[a-zA-Z][a-zA-Z0-9]*
Matlab[a-zA-Z][_a-zA-Z0-9]*
Mercury[_a-z][_a-zA-Z0-9]*
merd[_a-z][_a-zA-Z0-9]*[!?']*
Modula-3[a-zA-Z][_a-zA-Z0-9]*
MUMPS[a-zA-Z%][a-zA-Z0-9]*
OCaml[_a-z][_a-zA-Z0-9']*
Pascal[a-zA-Z][a-zA-Z0-9]*
Perl[_a-zA-Z0-9]+
Perl6[_a-zA-Z0-9]+
PHP[_a-zA-Z][_a-zA-Z0-9]*
PL/I[a-zA-Z][a-zA-Z0-9]*
Pliant[_a-zA-Z][_a-zA-Z0-9]* or '[^']*'
Prolog[_a-z][_a-zA-Z0-9]*
Python[_a-zA-Z][_a-zA-Z0-9]*
Rebol[_a-zA-Z?!.'+*&|=~-][_a-zA-Z0-9?!.'+*&|=~-]*
or
[^0-9[](){}":;/][^ \n\t[](){}":;/]*
Ruby[_a-zA-Z][_a-zA-Z0-9]*[!?]?
Scheme[_a-zA-Z!0&*/:<=>?^][_a-zA-Z!0&*/:<=>?^0-9.+-]*
SmallTalk[a-zA-Z][a-zA-Z0-9]*
SML[_a-z][_a-zA-Z0-9']*
Tcl[^ \t\n\r\f]+

   “Procedure names should reflect what they do; function names should reflect what they return.ÊFunctions are used in expressions, often in things like ifs, so they need to read appropriately.
        if(checksize(x))
   “is unhelpful because we can’t deduce whether checksize returns true on error or non-error; instead
        if(validsize(x))
   “makes the point clear and makes a future mistake in using the routine less likely.” —Rob Pike, Notes on Programming in C, February 21, 1989

mismatched types

    According to Brian Kernighan, co-creator of the AWK and AMPL programming languages and co-author of K&R C, one of the three most common errors for new programmers is mismatched types in a fucntion call.

Ada

    “15 A subprogram is the basic unit for expressing an algorithm. There are two kinds of subprograms: procedures and functions. A procedure is the means of invoking a series of actions. For example, it may read data, update variables, or produce some output. It may have parameters, to provide a controlled means of passing information between the procedure and the point of call. A function is the means of invoking the computation of a value. It is similar to a procedure, but in addition will return a result.” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

    “17 Subprogram and package units may be compiled separately and arranged in hierarchies of parent and child units giving fine control over visibility of the logical properties and their detailed implementation.” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

    “36 Access types allow the construction of linked data structures. A value of an access type represents a reference to an object declared as aliased or to an object created by the evaluation of an allocator. Several variables of an access type may designate the same object, and components of one object may designate the same or other objects. Both the elements in such linked data structures and their relation to other elements can be altered during program execution. Access types also permit references to subprograms to be stored, passed as parameters, and ultimately dereferenced as part of an indirect call.” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

    “43 Finally, the language provides a powerful means of parameterization of program units, called generic program units. The generic parameters can be types and subprograms (as well as objects and packages) and so allow general algorithms and data structures to be defined that are applicable to all types of a given class.” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

    “1 Ada is a programming language designed to support the construction of long-lived, highly reliable software systems. The language includes facilities to define packages of related types, objects, and operations. The packages may be parameterized and the types may be extended to support the construction of libraries of reusable, adaptable software components. The operations may be implemented as subprograms using conventional sequential control structures, or as entries that include synchronization of concurrent threads of control as part of their invocation. The language treats modularity in the physical sense as well, with a facility to support separate compilation.” —:Ada-Europe’s Ada Reference Manual: Section 1: General See legal information

BASIC

    The APPEND command is used in many forms of BASIC to combine a program or program parts from external storage (such as floppy disk or cassette tapes) to a program already in main memory. This can be used to bring in utility subroutines, to a large data set, or to bring in a large program in steps. The form is APPEND ProgramName. This command is sometimes called TAPPEND, MERGE, or WEAVE.

assembly language instructions

subroutine return pointer

    Some RISC processors include a special subroutine return pointer rather than using a stack in memory. The return address for subroutine calls is stored in this register rather than in memory. More than one level of subroutine calls requires storing and saving the contents of this register to and from memory.

See also Registers

other

   “Documentation that is structured and contained within the program is able to immediately satisfy the changing information demands of the maintainer. Those needs are determined in part by the subtask on which he is currently working. For solving nontrivial error correction or modification problems, the maintainer must have a detailed understanding of the program. To locate a section of code, knowledge of the program’s structure is required. Knowing how an instruction sequence relates to other parts of the program is important for altering and testing software. The documentor can inform the unknowledgeable programmer in each subtask demand by varying the message content and effectively using the visual space.
   “Information may be conveyed to the maintainer in several ways. One is an abstract summary of the module at the beginning of the routine. Another is through the titles and headings of processing sections positioned in the instruction sequence. The third is in phrases and short sentences to the right of the code. They describe the processing steps and relate them to other parts of the program. The descriptions are organized into an outline that reflects the processing divisions of the routine.
   “The size and complexity of the module determine whether the information will be used. Small routines may need only comments to the right of the code. A more complete description is required for large programs.
   “The type of documentation that has just been read has a bearing on the processing of code. Documentation formats act as advance organizers of thought. Each type primes the maintainer for a different response to the instructions encountered. Messages that are consistent with the structure of the program aid recognition and recall. … Programs are documented to enhance the maintainer’s performance.” —Dennis Smith. Designing Maintainable Software. Springer-Verlag, 1999, pg. 103

   “A second recommendation has to do with procedures and other major units of the program—Introductory module comments are also in order. Comments following a procedure header explaining the general nature of the procedure are not only in order but may be necessary. Keeping in mind the bridge aspect, we need not describe the calling environment. The professional assumes that the reader has read the program to the degree that the procedure calls are understood—but maybe not the procedure itself. As such, the procedure header comments should be short and help the reader understand the next level of detail in the program.” —Henry Ledgard. Professional Software Volume II: Programming Practice, Addison-Wesley, 1987, pg 65

   “6. Symmetry is a complexity reducing concept (co-routines include sub-routines); seek it everywhere.” —Alan Perlis, Epigrams on Programming, ACM’s SIGPLAN Notices Volume 17, No. 9, September 1982, pages 7-13

chapter contents


free music player coding example

    Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

    View music player in action: www.musicinpublic.com/.

    Create your own copy from the original source code/ (presented for learning programming).


return to table of contents
free downloadable college text book

view text book
HTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

previous page next page
previous page next page

free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,
then please donate some money.

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

    If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

more information on donating

Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.


Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2010, 2011, 2012 Milo

    Created: November 5, 2010

    Last Updated: February 16, 2012


return to table of contents
free downloadable college text book

previous page next page
previous page next page