Pascal - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Pascal Procedures

Pascal Procedures

shape Description

The procedure can also be known as sub program. A subprogram is nothing but a program module that plays out a specific assignment. All these subprograms are joined to frame bigger projects. This is essentially known as modular design. These subprograms will help the developer to evade redundancies. A strategy begin off with a begin and winds up with an end;. The procedure is the keyword which is used to define procedure, the following is the syntax. [c]procedure name(argument(s): type1, argument(s): type 2, ... );[/c] In the above syntax, argument is used to pass the values, these arguments will provide interface between calling program and functions identifiers so it is called as formal parameters. The following is an example for the procedures. [c]program exProcedure; var x, y, z, min: integer; procedure findMin(a, b, c: integer; var m: integer); (* Finds the minimum of the 3 values *) begin if a < b then m:= a else m:= b; if c < m then m:= c; end; { end of procedure findMin } begin writeln(' Enter three numbers: '); readln( x, y, z); findMin(x, y, z, min); (* Procedure call *) writeln(' Minimum: ', min); end.[/c] In the above example, x,y,z,min are the variables that have integer data type, where procedure created with the name findMin. Here the logic is if the value of a is less than b then print m otherwise print b. Output: Now compile the code result will be as follows. [c]Enter three numbers: 10,2,3 Minimum: 2[/c]

Example

shape Description

The following are the two programs that will perform with with repetition and also with out repetition by using sub procedure. splesson1.pas [c] (*The following is an example with repetition.*) Program splesson1; Uses Crt; Var Counter : Integer; Begin textcolor(green); GotoXy(10,5); For Counter := 1 to 10 do Begin Write(chr(196)); End; GotoXy(10,6); For Counter := 1 to 10 do Begin Write(chr(196)); End; GotoXy(10,7); For Counter := 1 to 10 do Begin Write(chr(196)); End; GotoXy(10,10); For Counter := 1 to 10 do Begin Write(chr(196)); End; Readkey; End.[/c] Programs utilizing the CRT unit won't be usable when input or output is being diverted on the charge line. For comparative reasons these are not usable as CGI-scripts for utilize with a webserver. The utilization of the CRT unit and the diagram unit may not generally be supported.The CRT unit is not string safe. On linux executing different projects that expect extraordinary terminal conduct won't work. The terminal may be placed in RAW mode, which will decimate most terminal copying settings. The CRT unit originates from a TP/Dos region. It is intended to deal with single-byte character. splesson2.pas [c]Program splesson2; Uses Crt; Procedure DrawLine; {This procedure helps me to avoid the rewriting the for loops} Var Counter : Integer; Begin textcolor(green); For Counter := 1 to 10 do Begin Write(chr(196)); End; End; Begin GotoXy(10,5); DrawLine; GotoXy(10,6); DrawLine; GotoXy(10,7); DrawLine; GotoXy(10,10); DrawLine; Readkey; End.[/c] The following are the various differences between above two programs. Repetitions: The repetitions in a code can bring about a tough scenarios for a developer. The procedures are a fundamental approach to stay away from repitions in a code. Size of the code:The code size may increase the performance where the first code has taken 1900 bytes and second code has taken 1350 bytes.

Summary

shape Key Points

  • If sub program calls itself only then it is called as recursive call.
  • The call by value and call by reference are used to pass the arguments.
  • The procedure is the keyword used to define a procedure.