What are Subprograms?


A subprogram is defined as a set of statements that can be reused at multiple places in a program when convenient. This reuse results in multiple types of savings, from memory space to coding time. Such reuse is also an abstraction, for the analysis of subprograms computations are restored in a program by a statement that calls the subprogram.

Features of Subprograms

The features of subprograms are as follows −

  • A subprogram has a single entry point.

  • The caller is suspended during the implementation of the called subprogram.

  • Control repeatedly returns to the caller when the called subprogram’s execution eliminates.

Types of Subprograms

There are two types of subprograms which are as follows −

  • Procedures − A procedure is defined as a subprogram that defines parameterized computations. These computations are executed by an individual call statement. Procedures represent new statements. For example, because Pascal does not have a sort statement, a user can develop a procedure to sort arrays of records and use a call to that procedure in place of the unavailable sort statement.

The general syntax of a procedure in Pascal is given as

PROCEDURE Name of Procedure (formal parameter list); {local declaration section}
BEGIN
{instruction sequence}
END;
{end of procedure}

The declaration implies that a procedure has two parts as the specification and the body. The procedure specification begins with the keyword PROCEDURE and end with the procedure name or a parameter list. Parameter declarations are optional. Procedures that take no parameters are written without parenthesis.

The procedure body begins with the keyword BEGIN and end with the keyword END followed by an optional procedure name. The procedure body has three elements such as a declarative part, an executable part, and an optional exceptional handling part.

  • Functions − A function is a subprogram that evaluates a value. Functions and procedures are structured identical, except that

    • Functions are semantically modeled on mathematical functions.

    • Functions have a RETURN clause.

    • Functions produce no side effects i.e., it changes neither its parameters nor any variables defined outside the function.

The general syntax of a function in C is given as

RETURN TYPE Name of Function (formal parameter list){
   local declaration section
   ……………….
   ……………….
   instruction sequence
}

A function has two elements as the specification and the body. The function specification begins with the Return type followed by name of the function and parameter list. The function body begins with {and ends with}. The function body has three parts such as a declaration part, an executable part, and an optional exceptional-handling part.

Updated on: 23-Oct-2021

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements