Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Units and Data §Program Unit Structure §Declarative Part: Data Structures §Procedural Part: Statements §Procedures §Calling the procedures or.

Similar presentations


Presentation on theme: "Program Units and Data §Program Unit Structure §Declarative Part: Data Structures §Procedural Part: Statements §Procedures §Calling the procedures or."— Presentation transcript:

1

2 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

3 Program Unit Structure §A program unit has a well defined structure §Consider the C++ programs that you have developed l #include l void main (void) l{l{ l}l} l Let us look at a short program

4 Cookie Monster #include void main(void) { int cookies; cookies=12; while (cookies>=1) { cout<<“Cookies are for me!!”<<endl; cookies--;} }

5 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

6 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

7 AN ARRAY OF CHARACTERS IS A ONE DIMENSIONAL ROW ABCDE ABCDE A CIRCULARLY LINKED LIST

8 Example of Tables

9 Heterogeneous Data With a Single Name

10 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) { }

11 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

12 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

13 A Program Unit Calling a Procedure

14 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

15 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)

16 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


Download ppt "Program Units and Data §Program Unit Structure §Declarative Part: Data Structures §Procedural Part: Statements §Procedures §Calling the procedures or."

Similar presentations


Ads by Google