Subroutine Call

Background:

In any programming language, to keep program code clean and understandable the program code is divided into chunk of code blocks performing some unit of task to achieve desired result. This chunk of code block is referred as subroutine. In some programming language it is called function, method procedure etc.

Below are the characteristic of a good subroutine:

1. Modularity:

  • Subroutines divide a program into smaller, manageable, and reusable blocks of code, making the program more modular.

2. Reusability:

  • Once defined, a subroutine can be called and reused multiple times within a program or even in different programs.

3. Abstraction:

  • Subroutines allow the implementation details to be hidden from the rest of the program. This helps in managing complexity by allowing the programmer to think at a higher level of abstraction.

4. Parameters and Arguments:

  • Subroutines can accept inputs known as parameters or arguments, enabling them to perform operations based on these inputs.

5. Return Values:

  • Subroutines can return values to the caller, providing the result of the operations performed within the subroutine.

6. Local Variables:

  • Variables declared within a subroutine are local to that subroutine and cannot be accessed outside it, thus maintaining a clear scope and avoiding name conflicts.

7. Control Transfer:

  • When a subroutine is called, control is transferred to the subroutine. After the subroutine completes its task, control returns to the point immediately following the call.

8. Encapsulation:

  • Subroutines encapsulate specific functionality, which makes the code easier to understand, test, and maintain.

9. Recursion:

  • A subroutine can call itself. This technique, known as recursion, is useful for solving problems that can be broken down into smaller, similar problems.

10. Efficiency:

  • While subroutines add overhead due to the control transfer and return mechanisms, they often lead to overall efficiency in terms of code maintainability and readability.
Execution of Subroutine:

A subroutine works by encapsulating a block of code that performs a specific task.

Based on below JAVA code we will try to understand execution of a subroutine
public class FuctionCall {

/**
* @param args
*/
public static void main(String[] args) {
int x=10,y=20;
int result=process(x,y);
System.out.println("Result is-"+result);
}

private static int process(int x, int y) {
int key=1;
return x+y+key;
}

}

1. Definition and Declaration:

  • Definition: The subroutine is defined with a specific name, parameters, and the code block that performs the desired task. Here process() is a subroutine.
  • Declaration: In some languages, the subroutine must be declared before it is used, specifying its name, return type, and parameters.

2. Calling the Subroutine:

  • When the program execution reaches a point where a subroutine is called, the control is transferred to the subroutine’s code block. But before control is transferred to subroutine's block following system level operations are performed.
    • Next instruction address which appears in the parent routine just after subroutine call is saved on the system stack memory area which is picked up for execution once subroutine block is executed successfully. In Our example from below symbolic picture, address of next instruction is 112 which is pushed on the system stack memory area.
    • State of parent routine is saved in the stack memory of the system. Means value of local variables e.t.c. are saved on the stack area of the system memory. Once subroutine is executed and control comes back to the parent subroutine, state of the parent routine is popped back from the system stack memory area and the parent routine execution is resumed. In our example, the state of routine main() is set of local variables in main() which are x, y and result.
  • The subroutine is invoked with arguments (actual values) corresponding to the parameters defined in its declaration. Below image shows execution of subroutine based on JAVA code mentioned above.


3. Execution of the Subroutine:

  • Local Environment: The subroutine creates its local environment, including local variables and any temporary storage needed.
  • Execution: The statements within the subroutine are executed in sequence, performing the specified task.

4. Returning Control:

  • Once the subroutine completes its task, it returns control to the point in the main program immediately after the subroutine call.
  • The subroutine may return a value to the caller, which can be used in the main program or other parts of the code.
Previous Post Next Post