Open In App

Subroutine, Subroutine nesting and Stack memory

In computer programming, Instructions that are frequently used in the program are termed Subroutines. This article will provide a detailed discussion on Subroutines, Subroutine Nesting, and Stack Memory. Additionally, we will explore the advantages and disadvantages of these topics. Let’s begin with Subroutines.

What is a Subroutine?

A set of instructions that are used repeatedly in a program can be referred to as a Subroutine. Only one copy of this Instruction is stored in the memory. When a Subroutine is required it can be called many times during the Execution of a particular program. A call Subroutine Instruction calls the Subroutine. Care Should be taken while returning a Subroutine as a Subroutine can be called from a different place from the memory.



The content of the PC must be Saved by the call Subroutine Instruction to make a correct return to the calling program. 

Process of a subroutine in a program 



The subroutine linkage method is a way in which computers call and return the Subroutine. The simplest way of Subroutine linkage is saving the return address in a specific location, such as a register which can be called a link register called Subroutine. 

Advantages of Subroutines

Disadvantages of Subroutines

What is Subroutine Nesting?

Subroutine nesting is a common Programming practice In which one Subroutine calls another Subroutine.

A Subroutine calling another subroutine 

From the above figure, assume that when Subroutine 1 calls Subroutine 2 the return address of Subroutine 2 should be saved somewhere. So if the link register stores the return address of Subroutine 1 this will be (destroyed/overwritten) by the return address of Subroutine 2. As the last Subroutine called is the first one to be returned ( Last in first out format). So stack data structure is the most efficient way to store the return addresses of the Subroutines. 

The Return address of the subroutine is stored in stack memory

What is Stack Memory?

A Stack is a basic data structure that can be implemented anywhere in the memory. It can be used to store variables that may be required afterwards in the program Execution. In a stack, the first data put will be the last to get out of a stack. So the last data added will be the first one to come out of the stack (last in first out)

Stack memory having data A, B & C

So from the diagram above first, A is added then B & C. While removing the first C is Removed then B & A.

Advantages of subroutine nesting and Stack Memory

Disadvantages of Subroutine Nesting and Stack Memory

Subroutine, Subroutine nesting and Stack Memory – FAQs

1. How subroutine nesting is handled with the help of stack?

Prior to calling the subroutine, the calling program places the four parameters onto the stack. Next, the call instruction is executed, causing the return address to be added to the stack. At this point, the stack pointer (SP) points to the return address, and the first instruction of the subroutine is ready to execute.

2. Why do we need stack and subroutine?

By using a stack, it’s possible to store multiple entries and have multiple subroutines active simultaneously. If one subroutine calls another, the microprocessor must keep track of the return addresses of both subroutines in the order they were called.

3. Does Subroutines with nested levels can be accessed from other subroutines?

Yes, subroutines having nested levels can easily access variables from other subroutines, just a condition that these variables are within the scope of the subroutine.

4. Why return statement is used in Subroutine?

The “return” statement is used to exit from a subroutine and it also provides value back to the calling code. It also maintains the flow of execution by giving control to the caller.

Article Tags :