Wednesday, October 17, 2018

Procedures and Functions Using Pascal

  • In computer programming, a subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
  • Subprograms may be defined within programs, or separately in libraries that can be used by many programs.
  • In different programming languages, a subroutine may be called as a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.
  • The content of a subroutine is its body, which is the piece of program code that is executed when the subroutine is called or invoked.
  • A function returns a value and a procedure just executes commands. The name function comes from math. It is used to calculate a value based on input. A procedure is a set of command which can be executed in order, even functions can have a set of commands but the difference is only in the returning a value part.
  • A subroutine may be written so that it expects to obtain one or more data values from the calling program.
  • The calling program provides actual values for these parameters, called arguments.
  • Different programming languages may use different conventions for passing arguments.
    Eg :
    Call by value : Argument is evaluated and copy of value is passed to subroutine,
    Call by reference : Reference to argument, typically its address is passed
  • Local data is accessible only from within the program in which the local data is declared. Local data is not visible or accessible to any program outside of the one where it is declared.
  • Global data is accessible from within the program in which the global data is declared or from within any other nested programs which are directly or indirectly contained in the program that declared the global data.

The advantages of breaking a program into subroutines
  • A complex programming task into simpler steps
  • Reducing duplicate code within a program
  • Enabling reuse of code across multiple programs
  • Hiding implementation details from users of the subroutine
  • Improving readability of code by replacing a block of code with a function call

Exercises Using Pascal



Procedure in Pascal with Global, Local Data without Parameter

var
 c:integer; {define global variable}

{procedure declaration with global,local data}
procedure ctof() ;
var
  f:real;   {define local variable}
begin
    f:=(c*9)/5+32;
    write('Celcious ',c,' = to farenheite',f:6:2);
end;

{main program}
begin
  write('Enter a celcious value?');
  readln(c);
  ctof();   {calling procedure}
  readln;
end.


Procedure in Pascal with Parameter - Call by value

var
  c:integer; {define global variable}

{procedure declaration with parameter }
procedure ctof(cc:integer) ;
var
  f:real;   {define local variable}
begin
    f:=(cc*9)/5+32;
    write('Celcious ',cc,' = to farenheite',f:6:2);
end;

{main program}
begin
  write('Enter a celcious value?');
  readln(c);
  ctof(c);   {calling procedure -> call by value}
  readln;
end.



Procedure in Pascal with Parameters - Call by Reference

{define Global data}
var
  c:integer;
  f:real; '

 {Define procedure with parameters -> Call by reference}
procedure ctof(cc:integer;var ff:real) ;
begin
    ff:=(cc*9)/5+32;
    write('Celcious ',cc,' = to farenheite',ff:6:2);
end;
{main program}
begin
  write('Enter a celcious value?');
  readln(c);
  ctof(c,f);   {calling procedure -> call by reference}
  readln;
end.


Function in Pascal with Parameter

{define Global data}
var
  c:integer;
  f:real;

 {Define function with parameters }
function ctof(cc:integer):real ;
var
  {local variable}
  ff:real;
begin
    ff:=(cc*9)/5+32;
    ctof:=ff;
end;
{main program}
begin
  write('Enter a celcious value?');
  readln(c);
  write('Celcious ',c,' = Fahrenheite ',ctof(c):5:2); {calling function with parameter}
  readln;
end. 

Watch Video with English Subtitle




1 comment:

  1. Refreshing memory of past education Polytechnic, programs from Basic, C+, Pascal, Fortran. Preparing for next level, possibly Cyber applications or security.

    ReplyDelete