0% found this document useful (0 votes)
4 views

Reference for Main Program_subroutine

The document explains the roles of a main program and subroutines in programming, emphasizing their importance for code organization, reusability, and readability. The main program serves as the entry point and controls execution flow, while subroutines perform specific tasks and can be reused throughout the program. Additionally, it describes the architecture of the Java Virtual Machine (JVM), detailing components such as the Class Loader, Method Area, Heap, and Execution Engine.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Reference for Main Program_subroutine

The document explains the roles of a main program and subroutines in programming, emphasizing their importance for code organization, reusability, and readability. The main program serves as the entry point and controls execution flow, while subroutines perform specific tasks and can be reused throughout the program. Additionally, it describes the architecture of the Java Virtual Machine (JVM), detailing components such as the Class Loader, Method Area, Heap, and Execution Engine.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Main program/

Subroutine
FOR REFERENCE ONLY
•In programming, a main program and a
subroutine (also called a function,
procedure, or method, depending on the
language) work together to structure a
program efficiently. They help improve
code organization, reusability, and
readability.
•The main program is the central part of
a software application that controls the
overall flow of execution. It is where the
execution starts and typically
coordinates the use of subroutines.
Key Characteristics of a Main
Program
• Entry Point: The execution of the program starts
from the main program.
• Controls Flow: It calls subroutines when needed.
• Manages Data Flow: It may pass data to subroutines
and handle returned values.
• Often Minimal Logic: Ideally, the main program
should not contain complex logic but should delegate
tasks to subroutines.
Example of a Main Program (Python)
• def add_numbers(a, b): # Subroutine
• return a + b

• def main(): # Main Program


• num1 = 10
• num2 = 20
• result = add_numbers(num1, num2) # Calling subroutine
• print("The sum is:", result)

• if __name__ == "__main__":
• main() # Execution starts here
Subroutine(Function/Procedure)
• A subroutine is a block of code that performs a
specific task. It is designed to be reusable and can be
called multiple times from different parts of the
program.
• Types of Subroutines
• Functions: Return a value after execution.
• Procedures (Void Functions): Perform an action but
do not return a value
Key Characteristics of a Subroutine

•Encapsulation: Groups related code into a single unit.


•Reusability: Can be called multiple times to
perform a task.
•Modularity: Helps in breaking down a large
program into smaller parts.
•Parameter Passing: Can accept input values
(arguments) and return results.
Example of a Subroutine (Python)
• def multiply_numbers(x, y): # Function with return value
• return x * y

• result = multiply_numbers(5, 4) # Calling the function


• print("Multiplication result:", result)
Example of a Subroutine (C++)
• #include <iostream>
• using namespace std;

• int multiply(int a, int b) { // Function with return value


• return a * b;
• }

• int main() {
• int result = multiply(5, 4); // Calling the function
• cout << "Multiplication result: " << result << endl;
• return 0;
• }
Thinks to remember….
• The main program is the entry point and controls
the overall execution.
• Subroutines help structure the program by
breaking it into smaller, reusable blocks.
• Using subroutines makes programs modular,
efficient, and maintainable.
Java Virtual Machine (JVM)
architecture
Class Loader
• Loads class files into the JVM.
• Responsible for linking classes and preparing them for execution.
Method Area

• Stores class metadata, including method code and runtime constants.


Heap
• Stores objects created during execution.

• Shared among all threads.


Java Stacks
• Each thread has its own stack.
• Stores frames that contain method calls, local variables, and partial
results.
PC (Program Counter) Registers

• Keeps track of the current instruction being executed by each thread.


Native Method Stacks

• Stores native method information, which is used when Java interacts


with native code (e.g., C, C++).
• Execution Engine
-Core of the JVM, responsible for executing bytecode.
-Works with memory areas (heap, stack, method area, etc.).
• Controller (Inside Execution Engine)
-Directs the execution of bytecode instructions.
• Instructions (A, B, C, Z, etc.)
-Represents different bytecode instructions being executed by the
engine.
Overall Flow
• Class files are loaded by the Class Loader.
• The loaded classes are stored in the Method Area.
• Objects are created and managed in the Heap.
• Each thread has its own Java Stack and PC Register.
• The Execution Engine fetches and executes instructions.
• If required, Native Method Stacks handle native code
execution.

You might also like