What is the difference between Subprograms and Coroutines?


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 continually returns to the caller when the called subprogram’s execution eliminates.

Advantages of subprograms

There are the following advantages of subprograms which are as follows −

  • Subprogram promotes reusability. It can reuse a set of statements at multiple places in a program.

  • Subprograms provide extensibility i.e., they let you tailor the language to suit your requirement. An example if you need a procedure that generates new departments, you can simply write one.

  • Subprogram promotes maintainability i.e., if its definition changes, only the subprogram is influenced. This facilitates maintenance and improvement.

  • Subprograms aid abstraction i.e., to use subprograms, you should understand what they do, not how they work. Thus, you can create applications from the top-down without concerned about execution details.

Coroutines

A coroutine is a special subprogram that has various entries. They can be used to provide interleaved execution of subprograms. In general terms, a coroutine is a special subprogram that returns to its calling program before the completion of execution.

A continue executes partially, then execution is suspended, control is returned to the caller, and execution is resumed later from the point of suspension. The first high-level language to include the facility for coroutines way SIMULA 67. Other languages that support coroutines are BLISS, INTER LISP and MODULA-2

Let us see the difference between Subprograms and Coroutines

SubprogramsCoroutines
A subprogram executes completely when called.
A coroutine executes partially when called.
Each time a subprogram is called, it begins execution from the beginning of the subprogram.
A coroutine resumes execution from the point at which the execution was previously suspended.
There is a master-slave relationship between a caller and callee subprogram.
Both caller and callee coroutines are treated on an equal basis and no master-slave relationship is present.
A subprogram has a single entry point.
A coroutine can have multiple entry points.
A subprogram is not history sensitive.
A coroutine must be history sensitive.
The invocation of a subprogram is called a subprogram call.
The invocation of a coroutine is known as coroutine resume.
It provides complete execution.
It provides interleaved execution.

Updated on: 23-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements