What is subroutine? Explain the steps of subroutine execution. Write a subroutine to find the sum of two given positive integers.
A routine or subroutine also referred to as a function, procedure, and subprogram is a portion of code that may be called and executed anywhere in a program. For example, a routine may be used to save a file or display the time. Instead of writing the code for these commonly performed tasks, routines are made and called when these tasks need to be performed.
- A subroutine is a set of instructions to perform a particular computation used to solve sub problems of more compile problems used for computations performed more than once in a single program or that are required in many different programs.
- You only need to write it once.
- Subroutines have names, zero or more parameters, return types, and a body of statements that perform the actual computations return-type subroutine-name( parameter-list ){… statements}
Steps in the execution of the procedure:
- Save return address,
- Procedure call ,
- Execute procedure,
- Return.