1 / 15

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

willhiter
Download Presentation

Program Units and Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

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

  4. 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

  5. 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

  6. AN ARRAY OF CHARACTERS IS A ONE DIMENSIONAL ROW A B C D E A B C D E A CIRCULARLY LINKED LIST

  7. Example of Tables

  8. Heterogeneous Data With a Single Name

  9. 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) { }

  10. 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

  11. 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

  12. A Program Unit Calling a Procedure

  13. 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

  14. 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)

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

More Related