Reading:
A procedure is a block of code that performs a specific task.
It is written once, and you can call (use) it many times in a program.
Think of a procedure like a small machine inside your program:
You give it some inputs (optional)
- It performs a task
- It may show output
- It does not return a value (that is a function)
⭐ Why Do We Use Procedures?
- To avoid repeating code
- To make programs easier to read
- To divide a big program into smaller logical parts
- To make debugging easier
- To reuse code again and again
⭐ Basic Structure of a Procedure
PROCEDURE ProcedureName()
// statements
ENDPROCEDURE
⭐ Example 1: A Simple Procedure That Displays a Message
PROCEDURE DisplayWelcome()
OUTPUT “Welcome to the Program”
ENDPROCEDURE
How to call the procedure:
CALL DisplayWelcome()
⭐ Example 2: A Procedure with 1 Parameter
A procedure can accept data to work with. This is called a parameter.
PROCEDURE ShowSquare(Number)
OUTPUT Number * Number
ENDPROCEDURE
Calling it Mian program:
CALL ShowSquare(5)
Output:
25
⭐ Example 3: A Procedure with 2 Parameters
PROCEDURE AddNumbers(A, B)
OUTPUT “Total = “, A + B
ENDPROCEDURE
Calling it in main program:
CALL AddNumbers(10, 20)
Output:
Total = 30
⭐ Example 4: Using a Procedure to Display a Menu
PROCEDURE DisplayMenu()
OUTPUT “===========Main Menu===========”
OUTPUT “1. Add Student”
OUTPUT “2. Delete Student”
OUTPUT “3. View List”
OUTPUT “4. Exit”
OUTPUT “===========Main Menu===========”
ENDPROCEDURE
// Main Program
CALL DisplayMenu()
⭐ Example 5: A Procedure Used Inside a Loop
PROCEDURE ShowMessage()
OUTPUT “Hello Student”
ENDPROCEDURE
FOR X <- 1 TO 3
CALL ShowMessage()
ENDFOR
Output
Hello Student
Hello Student
Hello Student
