Tuesday 29 December 2015

PASCAL PROGRAMMING

PROCEDURES AND FUNCTIONS

·       Modular programming is a technique used for writing large programs. The program is subdivided into small sections. Each section is called a module, and performs a single task.
·       Examples of tasks a module might perform are,
·        displaying an option menu
·        printing results
·        calculating average marks
·        sorting data into groups
·       A module is known by its name, and consists of a set of program statements grouped using the begin and end keywords. The module (group of statements) is executed when you type the module name.
·       Pascal uses three types of modules. The first two are called PROCEDURES, the other a FUNCTION.
·        Simple procedures do not accept any arguments (values or data) when the procedure is executed (called).
·        Complex procedures accept values to work with when they are executed.
·        Functions, when executed, return a value (ie, calculate an answer which is made available to the module which wants the answer)
Procedures help support structured program design, by allowing the independant development of modules. Procedures are essentially sub-programs.

SIMPLE PROCEDURES

·       Procedures are used to perform tasks such as displaying menu choices to a user. The procedure (module) consists of a set of program statements, grouped by the begin and end keywords. Each procedure is given a name, similar to the title that is given to the main module.
·       Any variables used by the procedure are declared before the keyword begin.
        PROCEDURE  DISPLAY_MENU;
          begin
               writeln('<14>Menu choices are');
               writeln(' 1: Edit text file');
               writeln(' 2: Load text file');
               writeln(' 3: Save text file');
               writeln(' 4: Copy text file');
               writeln(' 5: Print text file')
          end;
 
·       The above procedure called DISPLAY_MENU, simply executes each of the statements in turn. To use this in a program, we write the name of the procedure, eg,
 
        program PROC1 (output);
        
        PROCEDURE  DISPLAY_MENU;
        begin
             writeln('<14>Menu choices are');
             writeln(' 1: Edit text file');
             writeln(' 2: Load text file');
             writeln(' 3: Save text file');
             writeln(' 4: Copy text file');
             writeln(' 5: Print text file')
        end;
 
        begin
               writeln('About to call the procedure');
               DISPLAY_MENU;
               writeln('Now back from the procedure')
        end.
 
·       In the main portion of the program, it executes the statement
 
               writeln('About to call the procedure');
·       then calls the procedure DISPLAY_MENU. All the statements in this procedure are executed, at which point we go back to the statement which follows the call to the procedure in the main section, which is,
 
               writeln('Now back from the procedure')
·       The sample output of the program is
                About to call the procedure
                Menu choices are
                1: Edit text file
                2: Load text file
                3: Save text file
                4: Copy text file
                5: Print text file
                Now back from the procedure
 



No comments:

Post a Comment