You are on page 1of 28

How to deal with big software projects?

Modular design
• Modular design
– Modular design identifies the components of a
project that can be developed separately.
– Each module consists of a set of logical constructs
that are related to one another.
– A module may refer to one or
more other modules.
Modular design-Objectives
• Simplification
Modular design- example
• In groups of 5 think of a “Text editor”
software, i.e. Microsoft Office.
• List some of the modules in a text editor.
Modular design-Advantages
• The module is easy to upgrade.
• The module contains a readable amount of
code.
• The module may be used as part of the
solution to some other problem.
Modular design structure
• Each module has one entry point and one exit
point.
• Each module is highly cohesive.
• Each module exhibits low coupling.
Cohesion- example
• The following tasks are related
– receives a date and a number of days
– converts the date into a format for adding days to it
– adds the number of days received
– converts the result of the addition to a new date
– returns the new date
• The following tasks are unrelated
– calculates Federal tax on bi-weekly payroll
– calculates the value of π
– outputs an integer in hexadecimal format
Functions
• C supports modules through function syntax.
• When a function transfers control to another
function, we say that it calls that other
function. When the other function completes
its task and transfers control to the caller
function, we say that that other function
returns control to its caller.
Functions-Example1
• Write a function to print “Hello world!”. Call
that function from the main program.
Function Definition
Function-Example
• Write a function to print positive integers from
1 to 100. Call the function from the main
function!
Function-Example
• Write a function that takes two integers and
calculates the sum of the integers.

int sum(int a, int b);


Function-Example
• Write a function that takes two integers: base,
and power. Then, the function calculates the
result of raising the base to the power.
Function-Walkthrough
• The completed walkthrough table for
the power.c
What is the error of the following code?
int g(void)
{
printf(“inside function g\n”);
int h(void)
{
printf(“inside function h\n”);
}
}
What is the error of the following code?
int sum(int x, int y)
{
int result;
result=x+y;
}
Pass by value
• C passes data from a caller to a function by
value.
• Each parameter is a variable with its own
memory location.
• Pass by value facilitates modular design by
localizing consequences.
• The function being called may change the value
of any of its parameters many times, while the
values of the corresponding arguments in the
caller remain unchanged.
Pass by value- Example
#include <stdio.h>
int modify(int x);

int main() {
int i=10, newi;
printf(“i= %d",i);
newi= modify(i);
printf(“i= %d, newi= %d ",i,newi);
return 0;
}
int modify(int i){
i = i *1000;
printf(“i= %d“,i);
return i;
}
Prototype
• A function prototype provides the information
that the compiler requires to validate a
function call.
• A prototype is similar to a function header and
does not include the function body.

type identifier(type parameter, ..., type parameter);


Prototype
• We insert prototypes near the head of our
source file and before any function
definitions.
• If we declare a prototype for each function in
a source file, we can arrange our function
definitions in any order.
Prototype- Example
#include
• For any function defined outside our source
file, we store its prototype in a separate file
called a header file.
• Typically, a header file has the extension .h.

• #include <filename>
• #include "filename”
System Directories
• The header file that contains the prototypes
for the printf() and scanf() functions is called
stdio.h and is stored in the system directories.
Scope
• Global Scope
– A variable that we declare outside all function definitions
has global scope.
– The compiler allocates memory for a global variable
alongside the string literals at startup and releases that
memory at termination.
– #define Pi 80
• Local Scope
– A variable that we declare within a function body
has local scope.
– The scope of the variable extends from its declaration
statement to the end of the code block within which we
declared the variable.
Programming Style
• Limit the number of local variables to below 10, if
possible.
• Place the opening brace on a separate line.
• Declare a prototype for each function definition.
• Include parameter identifiers in the prototype
declaration.
• Use generic comments and variables names to
enable future use in different applications
without having to modify any of the function
code.
Documentation
• what the function does (not how)
• what the function needs (in terms of values
for its parameters)
• what the function returns (if anything)
Documentation-Example

You might also like