What is Subroutine ?

0
10
Subroutines begin with subroutine name (arg1, arg2,…), then the types of all arguments and local variables should be declared. Subroutines end with the statements return and end. Subroutines are called from another routine with the command call name (arg1, arg2,…).
Where it used:
If more than one value is to be returned to the main program then a subprogram called a subroutine is used.
When no value is returned to the main program but specific operations are performed like arranging a list of numbers in ascending order to transpose a matrix then subroutines are used.
Example:
Let us consider a program which have frequently requirement of the solution of a quadric equation. This program will return four values, the two are real parts and other two are imaginary parts of the roots. In this case a function subprogram cannot be used so here subroutine is required to write this program.
The general form of a quadratic equation is:
                         ax2 + bx + c = 0
Here if a = 0 then the above equation is not a quadratic and it becomes a linear equation.
The value of x is:
                         x real 1 = -b/c
If a is not equal to zero then discriminant = ( b2 – 4ac )
If discriminant is greater than or equal to zero then the roots are real as:
                        x real 1 =( – b + root(b2 – 4ac ))/ 2a
                        x real 1 =( – b – root(b2 – 4ac ))/ 2a
If the discriminant is less than zero then the roots will have real and imaginary parts both which are as:
                        x real 1 = x real 2 = -b/2a
                        x imaginary 1 = ( root( b2 – 4ac))/2a
                        and x imaginary 2 = – x imaginary 1
Program:
Output:
CALL statement
A subroutine is referenced by a CALL statement.
The form of a CALL statement is:
CALL sub (list)
list is an actual argument
Execution of a CALL statement results in
· association of actual arguments with the corresponding dummy arguments, and
· association of actual arguments with the corresponding dummy arguments, and
· the actions specified by the referenced subroutine.
When a CALL statement is executed, the referenced subroutine must be one of the subroutines specified in subroutine subprograms or by other means in the executable program.