Procedural Programming
BY: Mr. Zubair Hasan
Procedural Programming
Procedural
Programming also known as Structured
programming
struct Student {
string name;
int marks;
};
void printStudent(Student s) {
cout << s.name << " " << s.marks;
}
int main() {
Student s1;
s1.name = "Ali";
s1.marks = 85;
printStudent(s1);
}
Structures
A structure is a user-defined data type that groups multiple
related variables under one name.
Think of a student record.
A student has:
Name
Roll number
Marks
Age
Instead of writing separate variables:
string name;
int rollNo;
float marks;
int age;
Structures
We group them into one structure.
struct Student {
char name[20];
int rollNo;
float marks;
};
Problems
Poor Data Security: Data was exposed globally.
Difficult Maintenance: Changing one function could break
others.
Code Reusability Was Limited: Functions depended heavily
on shared data.
Real-World Modeling Was Hard: Real systems contain
entities: Procedural programming handled actions, not
entities.
Student
Bank Account
Car
Employee
Bank System in Structured Programming
#include <iostream>
Using namespace std;
char accountHolder[20];
float balance;
Int main()
{
}
void deposit(float amount) {
balance = balance + amount;
}
void withdraw(float amount) {
balance = balance - amount;
}
void printBalance() {
printf("Balance: %f", balance);
}
Problems
Poor Data Security
Any function can change it — even accidentally.
Easily hack the balance data and then modify it.
Difficult Maintenance
Suppose the bank adds a rule that balance must never go
below zero.
Now you must modify every function that changes balance.
Means rules are not centralized.
Problems
Code Reusability Was Limited
Now Bank needs multiple accounts.
char accountHolder[20];
float balance;
This supports only one account.
To add more:
float balance1;
float balance2;
float balance3;
Functions must change:
void deposit(int accountIndex, float amount);
Code becomes messy quickly.
Problems
Real-World Modeling Was Hard
In real life, a bank contains:
Accounts
Customers
Transactions
Employees
But structured programming thinks like:
deposit()
withdraw()
printBalance()
It models actions, not entities.
So relationships become confusing.
Example:
depositCustomerAccountTransactionRecordUpdate();