Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-dvmhs Total loading time: 0 Render date: 2024-09-27T03:45:20.521Z Has data issue: false hasContentIssue false

10 - Subprograms

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

Subprograms are perhaps the oldest form of abstraction and existed long before the introduction of high level languages. Subprograms enable a unit of code to be encapsulated and thereby reused and also enable the code to be parameterized so that it can be written without knowing the actual data to which it is to be applied.

In Ada, subprograms fall into two categories: functions and procedures. Functions are called as components of expressions and return a value as part of the expression, whereas procedures are called as statements standing alone.

As we shall see, the actions to be performed when a subprogram is called are usually described by a subprogram body. Subprogram bodies are declared in the usual way in a declarative part which may for instance be in a block or indeed in another subprogram.

Functions

Afunction is a form of subprogram that can be called as part of an expression. In Chapter 6 we met examples of calls of functions such as Day'Succ and Sqrt.

We now consider the form of a function body which describes the statements to be executed when the function is called. For example the body of the function Sqrt might have the form

function Sqrt(X: Float) return Float is

R: Float;

begin

-- compute value of Sqrt(X) in R

return R;

end Sqrt;

All function bodies start with the reserved word function and the designator of the function being defined. If the function has parameters the designator is followed by a list of parameter specifications in parentheses. Each gives the identifiers of one or more parameters, a colon and then its type or subtype. If there are several such specifications then they are separated by semicolons. Examples of parameter lists are

(I, J, K: Integer)

(Left: Integer; Right: Float)

The parameter list, if any, is then followed by return and the type or subtype of the result of the function. In the case of both parameters and result the type or subtype must be given by a subtype mark and not by a subtype indication with an explicit constraint; the reason will be mentioned in Section 10.7. (But we can give a null exclusion which concerns access types and will be discussed in Chapter 11.)

The part of the body we have described so far is called the function specification.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2014

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Subprograms
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.015
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Subprograms
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.015
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Subprograms
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.015
Available formats
×