Program Units and Data

Download Report

Transcript Program Units and Data

Program Units and Data







Program Unit Structure
Declarative Part: Data Structures
Procedural Part: Statements
Procedures
Calling the procedures or functions
Passing parameters
Parameter handling in procedure
Program Unit Structure
 A program unit has a well defined structure
 Consider the C++ programs that you have
developed





#include <iostream.h>
void main (void)
{
}
Let us look at a short program
Cookie Monster
•
•
•
•
#include <iostream.h>
void main(void)
{
int cookies;
•
•
•
•
cookies=12;
while (cookies>=1) {
cout<<“Cookies are for me!!”<<endl; cookies--;}
}
Data Declaration Part
 The first part of the program unit is called
the declarative part
 Here all the data items to be used in the
program are declared
 For example int cookies;
 Data items to be used in the program are
conceptually shaped according to the needs
Data Shapes
 Data can be kept in the form of one
dimensional arrays, two dimensional tables,
trees, linked lists and records
 There are appropriate data structures
provided in programming languages
AN ARRAY OF CHARACTERS IS A ONE DIMENSIONAL ROW
A B C D E
A B C D E
A CIRCULARLY LINKED LIST
Example of Tables
Heterogeneous Data With a
Single Name
Procedural Part
 The procedural part of program unit
contains two types of statements
 Assignment statements
 For example, cookies=12;
 Control statements
 For example, while (condition) { }
Procedures (Functions in C++)
 We focus on the program unit to understand
the way the modular software is developed
 Main program unit can call procedures to
carry out some work. In C++, these are
called functions
 In C++, the main program unit is our
 void main(void)
 It is where the execution begins
Procedures
 A procedure can be called from the main
program unit(In C++, any function can call
any other function)
 Each procedure or function has the same
structure as we have defined for program
unit. Each procedure or function contains
declarative part and procedural part
 Let us see the way the main program unit
calls a procedure
A Program Unit
Calling a Procedure
Procedure Call Steps
 The calling program unit reaches a certain
stage in execution where it needs to call a
procedure
 When the procedure call is executed,
control is transferred to the procedure
 The procedure carries out the work and then
transfers control back to the calling program
unit
Parameters
 When the program unit calls the procedure,
it can pass some data to be used by the
procedure
 For example, a procedure is to be called to
draw a circle on the screen
 The procedure must be told where to draw
the circle and how big the circle should be
 call draw_circle(center,radius)
Parameter Handling
 The data sent to the procedure can be
handled by the procedure in two ways
 The procedure can get a copy of the data. In
this way, the procedure can only read the
data and cannot modify it
 The procedure can get a reference to the
original data. In this case, the procedure can
modify the data