Correct option is D
The correct answer is D - Subroutines can have their own set of local variables.
Subroutines (also called functions, procedures, or methods) are independent blocks of code that can maintain their own local scope with private variables. These local variables exist only within the subroutine's execution context and are automatically created when the subroutine is called and destroyed when it returns. This concept of variable scope is fundamental to modular programming and helps prevent naming conflicts and maintains data encapsulation.
Important Key Points:
- Local Scope: Variables declared inside subroutines are accessible only within that subroutine.
- Stack Allocation: Local variables are typically stored on the call stack.
- Automatic Lifetime: Local variables are created and destroyed automatically.
- Data Encapsulation: Prevents interference between different parts of the program.
- Memory Management: Each subroutine call gets its own memory space for local variables.
- Variable Shadowing: Local variables can have the same name as global variables without conflict.
- Parameter Passing: Subroutines can receive parameters as local variables.
Knowledge Booster (Incorrect Options):
· Option A is incorrect because subroutines can return values to the calling program using return statements. This is a fundamental feature of functions in most programming languages.
· Option B is incorrect because subroutines can be called from any part of the program, including other subroutines, not just the main program. This enables nested function calls and recursive programming.
· Option C, while often true in practice, is not a defining characteristic of subroutines. Subroutines are used for code organization, reusability, and modularity - not exclusively for repetitive tasks. They can perform any computational task regardless of repetition.