# User-defined Functions in Pascal Standard Functions in Pascal

Document Sample

```					Pascal User-defined Functions                                                                               P.1

User-defined Functions in Pascal
Standard Functions in Pascal
Refer to P.58, 59 and 62 of the text book for some pre-defined functions in Pascal.
Refer to WS8 for the basic terminology and concepts of functions in Pascal.

User-defined Functions in Pascal
Syntax of Function Declaration
function <function name>(<parameter list>):<result type>;            -- function heading
const
<constant declaration>;
var
<variable declaration>;
begin
<function body>
end;

Note: The constants and variables declared in the function declaration section are local to the function.

Example:
Declaring and calling an user-defined function called Power(x,n) which raises x to the power n,
where x and n are integers.

program FunctionDemo1;
var                                                                -- declaration section of main
a, b: integer;                                                      program
aPb: real;
function Power(x, n: integer):real;                                -- function declaration section
var
p, i : integer;
begin
if n = 0 then Power:=1                                           -- a value is assigned to the
else                                                                function name Power
if n > 0 then
begin
p:=1;
for i:=1 to n do p:=p*x;
Power:=p                                              -- a value is assigned to the
end                                                       function name Power
else
begin
p:=1;
for i:=1 to -n do p:=p*x;
Power:=1/p                                            -- a value is assigned to the
end                                                       function name Power
end; {Power}
Pascal User-defined Functions                                                                        P.2

begin
write('Input a and b to find a^b : ');
aPb:=Power(a,b);                                                 -- a function call
writeln(a,'^',b,' = ',aPb:0:3)
end.

Sample Output 1:
Input a and b to find a^b: 3 4
3^4 = 81.000

Sample Output 2:
Input a and b to find a^b: -2 -3
-2^-3 = -0.125

Sample Output 3:
Input a and b to find a^b: 0 7
0^7 = 0.000

Sample Output 4:
Input a and b to find a^b: -9 0
-9^0 = 1.000

Note:
The function Power has two parameters (or arguments) of integer type, as indicated in the parameter
The data type real at the end of the function heading after the colon defines that Power returns a
real type result.
The result to be returned by a function is defined by assigning a value to the function name in the
function body. For example, the assignment statement Power:=1 assigns the value 1 to the function
name Power when n equals 0. As a result, Power(9,0) returns 1.
The formal parameters x, n and the variables p, i are local to the function.
Pascal User-defined Functions                                                                           P.3

Example: (A function with no parameters)

Program FunctionDemo2;
function HalfPi : real; (*no parameter list*)
begin
HalfPi := Pi/2; (*Pi is a Pascal pre-defined constant*)
end;
begin
writeln(Pi:0:10);
Writeln(HalfPi:0:10)
end.

Sample Output:
3.1415926536
1.5707963268

Note: The function HalfPi is not useful and put here for demonstration purpose. It is obvious that it can
be replaced by the constant declaration const HalfPi = Pi/2.

Example: (A function with a variable parameter)

program FunctionDemo3;
var
a, i : integer;
function double(var x : integer):integer;
begin
x := x * 2;
double := x
end;
begin
a := 2;
for i:=1 to 5 do
writeln(double(a));
end.

Sample Output:
4
8
16
32
64

Note: Similar to a Pascal procedure, a Pascal function can have value or variable parameters.
Pascal User-defined Functions                                                                              P.4

Exercise:
1.   Declare a Pascal function Area(a, b, c: real):real which calculates the area of a triangle
with given sides a, b and c according to the following formula:
a+b+c
area = s(s-a)(s-b )(s-c) where s =         ;
2
and write a program to accept 3 sides of a triangle and find its area by calling the function Area.
(Assume that a triangle can be formed by the sides a, b and c.)

program TriangleArea;
var
sa, sb, sc: real;
TriArea: real;

function Area(a, b, c : real): real;
var
s : real;
begin
s:=(a+b+c)/2;
Area:=sqrt(s*(s-a)*(s-b)*(s-c))
end;

begin
write('Input 3 sides: ');
TriArea:=Area(sa,sb,sc);
writeln('Area of triangle = ',TriArea)
end.

2.   Declare a Pascal function IsInteger(x : real):Boolean which checks whether the argument x
is an integer or not. The function IsInteger returns the Boolean value true when x is an integer
and false otherwise.
If IsInteger(a) then writeln('The number is an integer.');
If not IsInteger(a) then writlen('The number is not an integer.')

function IsInteger(x : real):Boolean;
begin
If trunc(x)=x Then IsInteger := TRUE
Else IsInteger := FALSE
end;
Pascal User-defined Functions                                                                                       P.5

Program Writing (Rewrite the program in WS10 using an user-defined function)
The program combination listed below evaluates C rn by calling the function factorial which
returns the factorial of an integer. Complete the program by filling in the missing statements.

Prerequisite Maths. Knowledge
Definitions:
For any positive integer k, k factorial, denoted by k! , is defined as   k!= k (k − 1)(k − 2) × ⋅ ⋅ ⋅ ⋅ ×2 × 1 .
n!
The number of combinations of taking r things from n different things, C rn =                    .
r!(n − r )!

Screen Output
** Find nCr **
Enter n : 5
Enter r : 3
5C3 = 10

The program in WS10 using the procedure factorial for your reference

program combination(input,output);
var
n,r,nCr : integer;
nf,             (* stores n! *)
rf,             (* stores r! *)
nmrf : longint; (* stores (n-r)! *)

procedure factorial(x:integer; var xf:longint); (* xf stores x! *)
var
i : integer;    (* loop counter *)
begin
xf := 1;
for i:=1 to x do
xf := xf*i
end; (* of factorial *)

begin (* main program *)
writeln('** Find nCr **');
writeln;
write('Enter n : ');
write('Enter r : ');
factorial(n,nf);
factorial(r,rf);
factorial(n-r,nmrf);
nCr := nf div rf div nmrf;
writeln;
writeln(n,'C',r,' = ',nCr)
end.
Pascal User-defined Functions                                   P.6

Program Using the Function factorial

program combination(input,output);
var
n,r,nCr : integer;

function factorial( x : integer ): longint;
var
i,                         (* loop counter *)
xf : integer;              (* stores x! *)
begin
xf := 1;
for i:=1 to x do
xf := xf*i;
factorial:=xf
end; (* of factorial *)

begin (* main program *)
writeln('** Find nCr **');
writeln;
write('Enter n : ');
write('Enter r : ');