Pascal guide - Functions & Procedures

   Ok back to today,
   today we will talk about FUNCTION and PROCEDURE.
   they are most used for top down  development and structured
   programming.

  first about procedure,
  this like a sub program/module, which build in a main program,
  it also contain a declaration part and content body ,like this:
declaraction   procedure IDENTIFIER(formal parameter list);
     part          var
                        .....
body             begin
                       ____
                       ___
                    end;
{remember ,this is a sub program,a semicolon is used at the "end"  of end}
let's see two programs explan about procedure:
sample 1:(not using procedure ,i am not explanning about looping!!)
program NOprocedure;
var
    I ,
    J:integer;
begin {main program}
    I:=1;
    j:=1;
    writeln('I and J is equal to',I ,' ',J);
    I:=I+1;
    j:=j+1;
    writeln('then I and J is equal to',I ,' ',J);
    I:=I+1;
    j:=j+1;
    writeln('then I and J is equal to',I ,' ',J);
  end.

now lets see a program with procedure replace the long structure
 like, I:=I+1;and J:=J+1;

sample 2:(using procedure)
program procedural;
var
    I ,
    J:integer;

procedure PLUS;
begin
 I:=I+1;
 J:=J+1;
end;

begin {main program}
    I:=1;
    j:=1;
    writeln('I and J is equal to',I ,' ',J);
    PLUS; {I:=I+1;and J:=J+1 replaced by plus)
    writeln('then I and J is equal to',I ,' ',J);
    PLUS;
    writeln('then I and J is equal to',I ,' ',J);
  end.

let's see a program using procedure with value and "var" parameters,
(this program is copy from book"element of Pascal programming"  by P.C. Chan and C. W. Wong of MERS,LTD)
sample3:
program main
var
 x,y:integer;
 temp:integer;

procedure Swap1;
begin
  Temp:=x;
   x:=y;
   Y:=temp
end;

procedre Swpa2(A,B:integer); {"var" not used in ( )}
begin
  Temp:=A;
   A:=B;
   B:=Temp;
end;

procedure Swap3(var A,B:integer);
begin
    Temp:=A;
    A:=b;
    B:=temp
end;

begin {Main program}
   x:=10;
   y:=20;
   write('Original values :');
   writeln('x =':26, X, ' Y =' :7 ,Y);  {the ":"(colon) is used to provide
space for output.}
   swap1;
   write('values after calling procedure Swap1:');
   writeln('X =':5, X, 'Y = ':7, Y);
   swap2(X,Y);
   write('values after calling procedure Swap1:');
   writeln('X =':5, X, 'Y = ':7, Y);
   swap3(X,Y);
   write('values after calling procedure Swap1:');
   writeln('X =':5, X, 'Y = ':7, Y);
end.
 

2.Function,
  Function like a formular,provided for calculation and string modifing.
  and will return a VALUE)
  there are many TP build in function we will talk about some of them
  in next day.
  let's see a program to show what is Function;
 sample 4:
 program CALC;
 var
 a,b,c:integer;

 function plus(a,b,c:integer);
 begin
  plus:=a+b+c;
 end;

begin
  a:=1;b:=3;c:=4;
  writeln(' Answer of A+B+C=',plus(a,b,c));
end.

the screen ouput will be:
 Answer of A+B+C=8

note:In C programming,only "function"
       a allow in programming,because
       a function of without return value (void)
       and have things to do is  equal to a
       procedure.

<< Previous || Next >>