UNIT 5
Data Flow Diagram (DFD)
A Data Flow Diagram (DFD) is typically used to represent the flow of data within a
system or process. Checking whether a number is even or odd is a simple computation that
can be represented using a DFD, although it may seem like overkill for such a straightforward
task. However, here's a basic DFD to illustrate how you could represent this process:
DFD Symbols Used:
Process: Represents the computation or action being performed.
Data Flow: Represents the flow of data between processes and data stores.
Data Store: Represents a place where data is stored.
Here's a simple DFD for checking whether a number is even or odd:
+-----------+ +-----------------+
Input --> | Check | -----> | Display Even |
Number | Even/Odd | | or Odd | --> Output
| Process | | Result |
+-----------+ +-----------------+
Explanation:
Input: This is the data flow representing the input, which is the number you want to
check (e.g., "Number" data).
Check Even/Odd Process: This is the process where the computation happens. It
checks whether the input number is even or odd.
Display Even or Odd Result: This process displays the result of the computation,
indicating whether the number is even or odd. The result is sent as output data.
Please note that in a real-world scenario, this simple task would typically be
implemented as a function or a few lines of code within a program rather than using a DFD.
Data Flow Diagrams are more commonly used for modeling complex systems and processes
with multiple inputs, outputs, and interactions.
DATA FLOW DIAGRAM (DFD)
A data flow diagram (DFD) is a graphical or visual representation using a
standardized set of symbols and notations to describe a business's operations through data
movement. They are often elements of a formal methodology such as Structured Systems
Analysis and Design Method (SSADM).
Characteristics of DFD
DFDs are commonly used during problem analysis.
DFDs are quite general and are not limited to problem analysis for software
requirements specification.
DFDs are very useful in understanding a system and can be effectively used during
analysis.
It views a system as a function that transforms the inputs into desired outputs.
The DFD aims to capture the transformations that take place within a system to the
input data so that eventually the output data is produced.
The processes are shown by named circles and data flows are represented by named
arrows entering or leaving the bubbles.
A rectangle represents a source or sink and it is a net originator or consumer of data.
A source sink is typically outside the main system of study.
Components of DFD
The Data Flow Diagram has 4 components:
Process Input to output transformation in a system takes place because of process
function. The symbols of a process are rectangular with rounded corners, oval, rectangle
or a circle. The process is named a short sentence, in one word or a phrase to express its
essence
Data Flow Data flow describes the information transferring between different parts
of the systems. The arrow symbol is the symbol of data flow. A relatable name should
be given to the flow to determine the information which is being moved. Data flow also
represents material along with information that is being moved. Material shifts are
modeled in systems that are not merely informative. A given flow should only transfer a
single type of information. The direction of flow is represented by the arrow which can
also be bi-directional.
Warehouse The data is stored in the warehouse for later use. Two horizontal lines
represent the symbol of the store. The warehouse is simply not restricted to being a data
file rather it can be anything like a folder with documents, an optical disc, a filing
cabinet. The data warehouse can be viewed independent of its implementation. When
the data flow from the warehouse it is considered as data reading and when data flows
to the warehouse it is called data entry or data updating.
Terminator The Terminator is an external entity that stands outside of the system
and communicates with the system. It can be, for example, organizations like banks,
groups of people like customers or different departments of the same organization,
which is not a part of the model system and is an external entity. Modeled systems also
communicate with terminator.
Rules for creating DFD
The name of the entity should be easy and understandable without any extra
assistance(like comments).
The processes should be numbered or put in ordered list to be referred easily.
The DFD should maintain consistency across all the DFD levels.
A single DFD can have a maximum of nine processes and a minimum of three
processes.
Symbols Used in DFD
Square Box: A square box defines source or destination of the system. It is also
called entity. It is represented by rectangle.
Arrow or Line: An arrow identifies the data flow i.e. it gives information to the
data that is in motion.
Circle or bubble chart: It represents as a process that gives us information. It is
also called processing box.
Open Rectangle: An open rectangle is a data store. In this data is store either
temporary or permanently.
Levels of DFD
DFD uses hierarchy to maintain transparency thus multilevel DFD’s can be created.
Levels of DFD are as follows:
0-level DFD: It represents the entire system as a single bubble and provides an
overall picture of the system.
1-level DFD: It represents the main functions of the system and how they interact
with each other.
2-level DFD: It represents the processes within each function of the system and how
they interact with each other.
3-level DFD: It represents the data flow within each process and how the data is
transformed and stored.
Advantages of DFD
It helps us to understand the functioning and the limits of a system.
It is a graphical representation which is very easy to understand as it helps visualize
contents.
Data Flow Diagram represent detailed and well explained diagram of system
components.
It is used as the part of system documentation file.
Data Flow Diagrams can be understood by both technical or nontechnical person
because they are very easy to understand.
Disadvantages of DFD
At times DFD can confuse the programmers regarding the system.
Data Flow Diagram takes long time to be generated, and many times due to this
reasons analysts are denied permission to work on it.
FUNCTIONS
Definition
A function can be defined as a subprogram which is meant for doing a specific task.
In a C program, a function definition will have name, parentheses pair contain zero or more
parameters and a body. The parameters used in the parenthesis need to be declared with type
and if not declared, they will be considered as of integer type.
The general form of the function is :
function type name <arg1,arg2,arg3, ————,argn>)
data type arg1, arg2,;
data type argn;
{
body of function;
——————————
——————————
——————————
return (<something>);
}
From the above form the main components of function are
• Return type
• Function name
• Function body
• Return statement
Return Type
Refers to the type of value it would return to the calling portion of the program. It can
have any of the basic data types such as int, float, char, etc. When a function is not supposed
to return any value, it may be declared as type void
Example
void function name(- - - - - - - - - -);
int function name( - - - - - - - - - - );
char function name ( — - - - - - - );
Function Name
The function name can be any name conforming to the syntax rules of the variable. A
function name is relevant to the function operation.
Example
output( ); read data( );
Formal arguments
The arguments are called formal arguments (or) formal parameters, because they
represent the names of data items that are transferred into the function from the calling
portion of the program.
Any variable declared in the body of a function is said to be local to that function,
other variable which were not declared either arguments or in the function body, are
considered “globol” to the function and must be defined externally.
Example
int biggest (int a, int b)
{
————————————
————————————
————————————
return( );
}
a, b are the formal arguments.
Function Body
Function body is a compound statement defines the action to be taken by the function.
It should include one or more “return” statement in order to return a value to the calling
portion of the program.
Example
int biggest(int a, int b)
{
if ( a > b)
return(a); body of function.
else
return(b);
}
The Return Statement
Every function subprogram in C will have return statement. This statement is used in
function subprograms to return a value to the calling program/function. This statement can
appear anywhere within a function body and we may have more than one return statement
inside a function.
The general format of return statement is
return;
(or)
return (expression);
If no value is returned from function to the calling program, then there is no need of
return statement to be present inside the function.
Advantages of Function
The main advantages of using a function are:
• Easy to write a correct small function
• Easy to read and debug a function.
• Easier to maintain or modify such a function
• Small functions tend to be self documenting and highly readable
• It can be called any number of times in any place with different parameters
CATEGORIES OF FUNCTIONS
A function, depending on, whether arguments are present or not and a value is
returned or not.
A function may be belonging to one of the following types.
1. Function with no arguments and no return values.
2. Function with arguments and no return values.
3. Function with arguments and return values
Advanced Featured of Functions
a. Function Prototypes
b. Calling functions by value or by reference
c. Recursion.
a. Function Prototypes
The user defined functions may be classified as three ways based on the formal
arguments passed and the usage of the return statement.
a. Functions with no arguments and no return value
b. Functions with arguments no return value
c. Functions with arguments and return value.
A. FUNCTIONS WITH NO ARGUMENTS AND NO RETURN VALUE
A function is invoked without passing any formal arguments from the calling portion
of a program and also the function does not return back any value to the called function.
There is no communication between the calling portion of a program and a called function
block.
Example:
#include <stdio.h>
main()
{
void message( ); Function declaration
message( ); Function calling
}
void message( )
{
printf (“GOVT JUNIOR COLLEGE \n”);
printf (“\t HYDERABAD”);
}
B. FUNCTION WITH ARGUMENTS AND NO RETURN VALUE
This type of functions passes some formal arguments to a function but the function
does not return back any value to the caller. It is any one way data communication between a
calling portion of the program and the function block.
Example
#include <stdio.h>
main()
{
void square(int);
printf (“Enter a value for n \n”);
scanf (“%d”,&n);
square(n);
}
void square (int n)
{
int value;
value = n * n;
printf (“square of %d is %d “,n,value);
}
C. FUNCTION WITH ARGUMENTS AND RETURN VALUE
The third type of function passes some formal arguments to a function from a calling
portion of the program and the computer value is transferred back to the caller. Data are
communicated between the calling portion and the function block.
CALLING FUNCTIONS BY VALUE OR BY REFERENCE
The arguments are sent to the functions and their values are copied in the
corresponding function. This is a sort of information inter change between the calling
function and called function. This is known as Parameter passing. It is a mechanism through
which arguments are passed to the called function for the required processing. There are two
methods of parameter passing.
1. Call by Value
2. Call by reference.
1. Call by value:
When the values of arguments are passed from calling function to a called function,
these values are copied in to the called function. If any changes are made to these values in
the called function, there are NOCHANGE the original values within the calling function.
2. Cal by Reference:
In this method, the actual values are not passed, instead their addresses are passed.
There is no copying of values since their memory locations are referenced. If any
modification is made to the values in the called function, then the original values get changed
with in the calling function. Passing of addresses requires the knowledge of pointers.
RECURSION
One of the special features of C language is its support to recursion. Very few
computer languages will support this feature. Recursion can be defines as the process of a
function by which it can call itself. The function which calls itself again and again either
directly or indirectly is known as recursive function.
The normal function is usually called by the main ( ) function, by mans of its name.
But, the recursive function will be called by itself depending on the condition satisfaction.
For Example,
main ( )
{
f1( ) ; ——— Function called by main
——————
——————
——————
}
f1( ) ; ——— Function definition
{
——————
——————
——————
f1( ) ; ——— Function called by itself
}
In the above, the main ( ) function s calling a function named f1( ) by invoking it with
its name. But, inside the function definition f1( ), there is another invoking of function and it
is the function f1( ) again.
Example programs on Recursion
//Write a program to find the factorial of given non-negative
integer using recursive function.
#include<stdio.h>
main ( )
{
int result, n;
printf( “ Enter any non-negative integer\n”);
scanf ( “ %d”, & n);
result = fact(n);
printf ( “ The factorial of %d is %d \n”, n, result);
}
fact( n )
int n;
{
int i ;
i = 1;
if ( i = = 1) return ( i);
else
{
i = i * fact ( n - 1);
return ( i );
}
}
FILE BASICS
File Operations like fopen(), fclose(), fprint(), fscan()
The help of these functions the user can open a file with the data file specifications,
create and write information in to the file and can close the file.
The following are the file processing related functions.
a. FILE OPEN function fopen()
b. FILE CLOSE function fclose()
c. FILE INPUT functions getc() and fscanf()
d. FILE OUTPUT functions putc() and fprintf()
A. THE FUNCTION fopen()
This function is used to open a data file. Moreover, this function returns a pointer to a
file. The use of the function is
file pointer = fopen( filename, mode);
Where file pointer is a pointer to a type FILE, the file name of the file name is name
of the file in which data is stored or retrieved (should be enclosed within double quotes) and
the mode denotes the type of operations to be performed on the file (this also to be enclosed
within double quotes). But, before making this assignment ,the file pointer and fopen() should
be declared as FILE pointer type variables as under :
FILE *file pointer, * fopen() ;
The mode can be one of the following types.
MODE MEANING
“r “ - read from the file
“w” - write to the file
“a” - append a file ie new data is added to the end of file
“ r+” -, open an existing file for the sake of updation.
“w +” - create a new file for reading and writing
“a +” - open a file for append, create a new one if the file does not exist
already
Examples
1. fptr = fopen(“rk. Dat”,”w”);
2. file= fopen(“sample.dat”, “r +”);
B. THE FUNCTIONS fclose ()
The files that are opened should be closed after all the desired operations are
performed on it .This can be achieved through this function .The usage of this function is:
fclose (file pointer);
Where file pointer is returned value of the fopen () function.
Examples:
1. fclose ( input file); 2. fclose (output file);
C. THE FUNCTIONS getc() & fscanf()
1. getc ( ) functions: this function is used a single character from a given
file ,whenever a file is referenced by a file pointer. The usage of this function is
getc (file pointer);
2. fscanf ( )function : This function is used to read a formatted data from a specified
file. The general usage of this function is
fscanf (f ptr, “Control String”, & list);
where fptr a file pointer to receive formatted data
Control string data specifications list
List the list of variables to be read
fscaf (infile , “%d %d ,” & no, &marks);
D. THE FUNCTIONS putc()& fprint()
Example
1. putc( ) function: This function is used write a single character into a file referenced
by the file pointer. The usage of this function is
putc (ch, file pointer),
Where
ch - the character to be written
file pointer - a file pointer to the file that receives the character.
2. fprintf () function: this function is used t write a for matted data in to a given file.
The specified information is written on the specified file.
The general form of usage for this function is:
fprintf (fptr, “Control String”, list):
Where
Fptr file pointer to write a formatted data
Control string data specifications list
list- list of variables to be written.
Example
fprintf (out file,”%d %f”, basic , gross);