CIT217 SEQUENTIAL
PROGRAMMING
MODULE 2
Key Points and Definitions: Computer Architecture Models
Computer Architecture:
o The style of construction and organization of the many parts
of a computer system.
Von Neumann Model:
o A common form of conventional digital computers.
o Consists of five major components:
1. Input Unit – Provides instructions and data to the
system.
2. Memory Unit – Stores instructions and data.
3. Arithmetic and Logic Unit (ALU) – Processes data.
4. Control Unit (CU) – Directs operations.
5. Output Unit – Sends results to the user.
o The ALU and CU together form the Central Processing Unit
(CPU), which controls operations and performs data
processing.
System Bus Model:
o A refinement of the Von Neumann model that partitions a
computer system into three subunits:
1. CPU (combining ALU and CU)
2. Memory
3. Input/Output (I/O) unit
o Components communicate through a system bus, a shared
transmission medium.
o A bus is a communication pathway connecting two or more
devices.
Types of Bus Lines:
1. Data Bus:
Carries the information being transmitted.
The width of the data bus determines how many bits
can be transferred at a time and affects system
performance.
2. Address Bus:
Identifies the source or destination of data on the data
bus.
The width of the address bus determines the maximum
possible memory capacity.
Also used to address I/O ports.
3. Control Bus:
Controls access to the data and address buses.
Transmits command and timing signals between system
components.
4. Power Distribution Lines:
Supplies power to attached modules.
Levels Within Computer Architecture
Hierarchic System:
o A computer is a complex system made up of interrelated
subsystems.
o A hierarchic system consists of multiple levels, where each
level abstracts details from the one below.
Seven Levels of a Computer System:
1. User Level (Application Programs):
The level at which the end user interacts with the
computer through programs such as word processors
and games.
2. High-Level Language Level:
Involves programming languages like C, Pascal,
FORTRAN, Java.
The programmer interacts with the system through the
language, while the compiler translates it into machine
instructions.
3. Assembly Language/Machine Code Level:
Directly controls how software interacts with hardware.
Uses mnemonics like MOVE Data, Acc, which are
translated into binary (1s and 0s) by an assembler.
4. Hardwired Control (Machine Layer):
The control unit executes machine instructions by
transferring data between registers using control
signals.
Hardwired control units are fast but difficult to modify.
5. Functional Unit (Microprogrammed Layer):
Executes machine instructions by directing digital logic
elements.
Includes CPU registers, ALU, and main memory.
6. Logic Gates (Digital Logic Layer):
Logic gates form functional units and execute logical
operations required for computation.
7. Physical Layer (Transistors and Wires):
The lowest level, consisting of electrical components
such as transistors and wires.
Deals with voltage, current, and signal propagation
rather than computational functions.
MODULE 3: PROGRAMMING LANGUAGES
UNIT 1: BLOCK STRUCTURED LANGUAGES
3.2 Subprograms
🔹 What is a Subprogram?
A subprogram is like a mini-program inside a bigger program. Think of
a full program like a house, and a subprogram like a room within that
house. Each room (subprogram) has a specific purpose—like the kitchen
is for cooking, and the bedroom is for sleeping.
So, in programming:
The house = your entire software or application.
A room = a subprogram that does one specific job (e.g.,
calculating age, sorting a list, or displaying data).
🔹 Key Points from the Text
1. Used loosely:
Sometimes, people just use the word “subprogram” to mean any
group of code that does something specific. But that’s not the strict
definition.
2. Proper subprogram should be:
o Self-contained: It should be complete in itself—like a room
that works without needing to know what’s happening in the
rest of the house.
o Performs well-defined operations: It should do a clear,
specific job.
o Uses well-defined data: It should clearly know what kind of
input it works with.
o Independent: Its internal working should not depend on the
rest of the program. That means you can use or reuse it
anywhere.
✅ When it has all these features, we can call it a program module.
🔹 Why are Subprograms Important?
Subprograms are the fundamental building blocks of programs.
Just like you build a house brick by brick, you build a program using
subprograms. They:
Help organize code
Make it easier to read
Allow you to reuse code (write once, use many times)
Make debugging and testing simpler
🧠 Analogy Time
Imagine you're organizing a birthday party (your main program).
You break it down into smaller tasks:
Send invitations ✅
Order the cake ✅
Set up decorations ✅
Each of these tasks is like a subprogram—they’re specific, separate, and
necessary parts of the big event.
If you're learning Python or any language, you'll see subprograms as:
Functions (e.g., def greet():)
Procedures (in some languages like Pascal)
Methods (when part of classes in OOP)
Want me to show you a real example of a subprogram in Python to
connect this better?
4o
You said:
yes
ChatGPT said:
Awesome! Let me show you a simple, real-life example of a subprogram
in Python.
🎯 Scenario: Greeting a user
Let’s say you want your program to greet someone by name.
Instead of writing the greeting code again and again, you create a
subprogram (in Python, this is a function) that does it.
🧩 Example in Python:
python
CopyEdit
# This is our subprogram (a function)
def greet_user(name):
print(f"Hello, {name}! Welcome to the system.")
# Main program
greet_user("Collins")
greet_user("Desire")
greet_user("Ada")
🔍 Explanation:
def greet_user(name): is a subprogram.
It takes one input (name) and prints a greeting.
It’s self-contained, has a clear purpose, and you can use it
anywhere in your program.
Instead of repeating print("Hello...") multiple times, you just call
greet_user() with the name you want.
✅ Why this is good:
It makes the code shorter and clearer.
You can change the greeting in one place, and it will update
everywhere.
You can reuse it in other programs too.
Let me know if you’d like to try writing your own subprogram now—or if
you want a more advanced example (maybe with calculations, user input,
or conditions).
4o
You said:
more examples of subprograms e.g function
ChatGPT said:
Great! Let's go through more examples of subprograms (functions)
in Python, with different real-world uses—from math to decision making.
These will help you get more comfortable with the concept.
🧮 1. A Subprogram That Adds Two Numbers
python
CopyEdit
def add_numbers(a, b):
return a + b
# Main program
sum_result = add_numbers(5, 3)
print("Sum is:", sum_result)
📝 Purpose: Adds two numbers and returns the result.
🎯 Benefit: You can reuse this to add any two numbers anywhere in your
code.
🎂 2. A Subprogram That Calculates Age
python
CopyEdit
def calculate_age(current_year, birth_year):
return current_year - birth_year
# Main program
age = calculate_age(2025, 2000)
print("Age is:", age)
📝 Purpose: Figures out someone’s age.
🎯 Benefit: If you change the formula later, you only edit the function—not
every place it’s used.
3. A Subprogram That Checks Temperature
python
CopyEdit
def check_temperature(temp):
if temp > 37.5:
print("Fever detected!")
else:
print("Normal temperature.")
# Main program
check_temperature(38.2)
📝 Purpose: Makes a decision based on temperature.
🎯 Benefit: Simplifies logic and makes code more readable.
🎲 4. A Subprogram That Simulates a Dice Roll
python
CopyEdit
import random
def roll_dice():
return random.randint(1, 6)
# Main program
print("You rolled:", roll_dice())
📝 Purpose: Returns a random number from 1 to 6.
🎯 Benefit: You can use this in games, tests, or random selection.
💡 5. A Subprogram That Tells if a Number is Even or Odd
python
CopyEdit
def is_even(number):
return number % 2 == 0
# Main program
num = 7
if is_even(num):
print(num, "is even")
else:
print(num, "is odd")
📝 Purpose: Checks for even numbers.
🎯 Benefit: Keeps logic clean, especially when used in bigger programs.
🔁 BONUS: Functions Can Call Other Functions
python
CopyEdit
def greet(name):
print(f"Hi {name}!")
def welcome_user(name):
greet(name)
print("You're now logged in.")
# Main program
welcome_user("Collins")
Here, welcome_user() uses another function called greet(). This shows
how subprograms can work together—just like a team!
Let me know if you'd like:
Examples with user input
Examples using loops or lists
A small project combining multiple subprograms (like a calculator or
ATM simulation)
Ready to try writing one yourself? I can guide you step by step.
4o
You said:
other examples outside function
ChatGPT said:
Great question, Collins! You're asking about subprograms beyond just
functions, and that shows deeper thinking.
So let's clarify something first:
✅ Subprograms are not limited to just functions.
In programming, subprograms generally include:
Functions
Procedures (used in some languages)
Methods (functions inside classes in OOP)
Modules
Classes (in some contexts)
Even event handlers or callbacks
Let me show you examples of subprograms outside basic functions,
especially in Python and general programming.
🧱 1. Methods inside a Class (Object-Oriented Programming)
python
CopyEdit
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
# Main program
calc = Calculator()
print("Addition:", calc.add(10, 5))
print("Subtraction:", calc.subtract(10, 5))
📝 What’s the subprogram?
The methods add() and subtract() are subprograms inside a class. We
call them methods instead of functions.
🧩 2. Modules (Code Split Across Files)
Imagine you have a file named math_utils.py:
python
CopyEdit
# math_utils.py
def square(x):
return x * x
Then in your main program:
python
CopyEdit
# main.py
import math_utils
print(math_utils.square(4))
📝 What’s the subprogram?
The function square() is part of a module (math_utils). You can reuse it in
any other program just by importing the file.
🔄 3. Lambda Functions (Anonymous Subprograms)
python
CopyEdit
square = lambda x: x * x
print(square(6))
📝 What’s the subprogram?
This is a lambda function—a mini one-line function that doesn’t need
the def keyword.
⚡ 4. Event Handlers (in GUI or Web Apps)
Example with Tkinter (Python GUI):
python
CopyEdit
from tkinter import *
def say_hello():
print("Hello!")
window = Tk()
button = Button(window, text="Click Me", command=say_hello)
button.pack()
window.mainloop()
📝 What’s the subprogram?
say_hello() is a subprogram that is called when the button is clicked—
this is an event handler.
🔁 5. Callbacks (Functions Passed as Arguments)
python
CopyEdit
def apply_operation(x, y, operation):
return operation(x, y)
def multiply(a, b):
return a * b
result = apply_operation(4, 5, multiply)
print("Result:", result)
📝 What’s the subprogram?
Here, multiply() is passed as a callback subprogram to apply_operation().
💼 6. Procedures (in other languages)
In Pascal or Ada, a procedure is like a function but doesn't return a value.
pascal
CopyEdit
procedure Greet(name: string);
begin
writeln('Hello, ', name);
end;
This is still a subprogram, just in a different language and format.
⚠️Summary Table:
Type of Where it's Returns Reusabl
Notes
Subprogram Used Value? e?
Function Most languages Yes ✅ Most common
Just performs an
Procedure Pascal, Ada No ✅
action
Inside Classes
Method Yes/No ✅ Tied to an object
(OOP)
One-line
Lambda Python, JS, etc. Yes ✅
anonymous
Group of related
Module Across files Yes ✅
functions
Type of Where it's Returns Reusabl
Notes
Subprogram Used Value? e?
Runs on user
Event Handler GUI/Web apps No ✅
action
Functional Passed into
Callback Yes ✅
programming another function
🧩 General Characteristics of Subprograms
i. Single Entry Point
The program can only enter the subprogram from one place: the
start.
🧠 Analogy: Like a room with only one door—you can't jump through
a window.
ii. Caller is Suspended During Execution
When a subprogram is called, the program pauses the current
activity and runs the subprogram.
🧠 Analogy: If you're cooking and the phone rings, you pause cooking
to answer. You only go back to cooking after the call ends.
iii. Control Returns After Execution
When the subprogram is done, it automatically returns control
to the point in the code where it was called.
🧠 Analogy: Like visiting a friend's house—when you’re done, you go
back to your house.
🧠 Basic Terminologies in Subprograms
i. Subprogram Definition
This is the full description of what the subprogram does.
Example in Python:
python
CopyEdit
def greet(name): # This whole thing is the definition
print("Hello", name)
ii. Subprogram Call
This is where you use or run the subprogram.
python
CopyEdit
greet("Collins") # This is a call
iii. Active Subprogram
A subprogram is active when it’s currently running, but hasn't
finished yet.
For example, during the greet("Collins") call, greet is active until the
print() finishes.
iv. Subprogram Header
The first line of the subprogram definition. It includes:
o The type (function/procedure)
o The name
o The parameters (if any)
💻 Header Examples:
Langua
Header Example
ge
FORTRA Subroutine
N Adder(parameters)
procedure
Ada
Adder(parameters)
C void Adder(parameters)
Python def Adder(parameters):
v. Parameter Profile (Signature)
This is the list of parameters, including:
o Number of parameters
o Order
o Type of each
🧾 Example:
CopyEdit
int add(int a, int b); // Signature: (int, int)
vi. Protocol
The signature + return type.
Think of this as the full contract of the subprogram.
🧾 Example:
CopyEdit
int add(int a, int b); // Protocol = Signature + return type (int)
vii. Subprogram Declaration
A declaration is like a promise that the function exists, but you
don’t give the full body yet.
Example in C:
CopyEdit
int add(int a, int b); // Declaration
Later, you define it:
CopyEdit
int add(int a, int b) { return a + b; } // Definition
viii. Formal Parameters
These are the placeholders or variables listed in the subprogram
header.
Example:
python
CopyEdit
def greet(name): # 'name' is a formal parameter
ix. Actual Parameters
These are the real values or variables you send when calling the
subprogram.
Example:
python
CopyEdit
greet("Collins") # "Collins" is the actual parameter
✅ TL;DR Summary
Term Meaning
A self-contained unit that performs
Subprogram
a task
Definition The full code of the subprogram
When the subprogram is used or
Call
executed
Active When the subprogram is running
Header The first line (name + parameters)
Formal
Placeholder in the header
Parameter
Actual
Real value passed during call
Parameter
Term Meaning
Parameter Number, order, and types of
Profile parameters
Protocol Parameter profile + return type
Declaration Signature without body