0% found this document useful (0 votes)
3 views25 pages

Modular Programming

Modular programming involves breaking down complex problems into simpler subproblems to facilitate easier coding and problem-solving. It utilizes top-down design, where a general problem is divided into subproblems, which can be implemented as subprograms like procedures and functions. Procedures perform actions without returning values, while functions return values, and both can accept parameters to enhance their functionality.

Uploaded by

manojones43
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

Modular Programming

Modular programming involves breaking down complex problems into simpler subproblems to facilitate easier coding and problem-solving. It utilizes top-down design, where a general problem is divided into subproblems, which can be implemented as subprograms like procedures and functions. Procedures perform actions without returning values, while functions return values, and both can accept parameters to enhance their functionality.

Uploaded by

manojones43
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULAR PROGRAMMING

2 - SMX
MODULAR PROGRAMMING
● Writing a program requires a previous step where we must reflect on what we
really want to do and how our goal should be reached.
● We are not sitting directly in front of the computer and starting to write lines of
code.
● We need a mechanism to solve problems with different degrees of complexity. This
mechanism consists in diveding it into a set of simpler problems. If we can solve
the simplest ones, then we also can solve the complex ones.
MODULAR PROGRAMMING
● Therefore, it is necessary to break down the task into simpler ones, which will be
solved one by one.
● There are two basic strategies for solving the decomposition of a problem:
○ Top-down. This is the most used.
○ Bottom-up.
MODULAR PROGRAMMING
Top-Down Design
● Top-down design is the technique based on a general problem and dividing it into
simple problems, called subproblems.
MODULAR PROGRAMMING
Top-Down Design
● Each subproblems that forms its own entity or a specific idea.
● Each one can be called several times in a program, with the mission of not
repeating code.
● Divide the code of complex program into subprograms (functions), each of them
specialized in solving a part of the problem.
MODULAR PROGRAMMING
Top-Down Design
● Features of the subprograms :
○ They are defined by a unique name that represents the block of code.
○ They can be called (executed) from any part of the code.
○ Values can be passed to them because they process them in some way.
○ They can return a result to be used from where it was called.
MODULAR PROGRAMMING
SUBPROGRAMS
As we already know, subprograms are pieces of code that perform a specific task and
that we can reuse. They help us to:

● Better organize the program


● Avoid repeating code
● Make the program more readable and maintainable
MODULAR PROGRAMMING
SUBPROGRAMS
There are two main types:
● Procedures. A procedure is a subprogram that performs actions, but does
NOT return any value.
● Functions. A function is a subprogram that RETURNS a value.
MODULAR PROGRAMMING
PROCEDURES
A procedure is a subprogram that performs actions, but does NOT return any value.
Features
● Executes instructions.
● Can receive parameters.
● Does not return any data; only performs an action (display, calculate, save,
etc.).
MODULAR PROGRAMMING
PROCEDURES
STRUCTURE
MODULAR PROGRAMMING
PROCEDURES
EXEMPLE
MODULAR PROGRAMMING
FUNCTIONS
A function is a subprogram that RETURNS a value.
Features:
● Returns a data (number, text, boolean…).
● Can receive parameters.
● Must always have a return instruction.
MODULAR PROGRAMMING
FUNCTIONS
STRUCTURE
MODULAR PROGRAMMING
FUNCTIONS
EXEMPLE

To call it
MODULAR PROGRAMMING
PARAMETERS
Subprograms can have parameters, which are data that it receives to work with.
● Parameters by value
○ The function/procedure receives a copy.
○ If you modify it, it does not affect the original.
● Parameters by reference
○ It works directly on the original variable.
○ If you modify it, the original also changes.
○ Used mainly in vectors/matrices
MODULAR PROGRAMMING
PARAMETERS
Subprograms can have parameters, which are data that it receives to work with.
● Parameters by value
○ The function/procedure receives a copy.
○ If you modify it, it does not affect the original.
● Parameters by reference
○ It works directly on the original variable.
○ If you modify it, the original also changes.
○ Used mainly in vectors/matrices
MODULAR PROGRAMMING
WHEN TO USE EACH

Procedure
When I want to do something (show data, modify a vector…).

Function
When I want to obtain a result (maximum, minimum, average, validation…).
MODULAR PROGRAMMING
WHEN TO USE EACH
EXEMPLE - PROCEDURE
MODULAR PROGRAMMING
WHEN TO USE EACH
EXEMPLE - FUNCTION
MODULAR PROGRAMMING
PROBLEM EXEMPLE
Read 5 student grades, calculate the average and show whether it is passed or failed.
First, you need to analyze and determine what the program does from a general point of
view:
1. Read the grades
2. Calculate the average
3. Show the result
MODULAR PROGRAMMING
PROBLEM EXEMPLE
Now, it is necessary to identify the functions and/or procedures
● Function 1: ReadNotes()
○ Reads the notes and saves them in a vector
○ Returns the vector with the 5 notes
● Function 2: CalculateAverage(grades)
○ Receives the vector
○ Returns the average
MODULAR PROGRAMMING
PROBLEM EXEMPLE

● Procedure 3: ShowResult(average)
○ Get the average
○ Show "pass" or "fail"
MODULAR PROGRAMMING
PROBLEM EXAMPLE
First we define the functions and/or procedures
MODULAR PROGRAMMING
PROBLEM EXEMPLE

And finally we define the main program


MODULAR PROGRAMMING

THANKS!!

You might also like