After covering PROCEDURE in Pascal, this time we'll look at using and writing FUNCTION in Pascal.
Declaring and Structuring a FUNCTION
You declare a FUNCTION in the same place as a PROCEDURE, right before the begin part of the main program. Both have roughly the same structure. Only one thing separates them, namely the data type of the return value in a FUNCTION.
The format for writing a FUNCTION in Pascal.
procedure <function-name>(<parameter>: <data-type>): <return-data-type>;
// local declarations
begin
// function body
end;
The fundamental difference between a PROCEDURE and a FUNCTION is the return value. A sub program that does not return a value is called a PROCEDURE, while one that returns a value is called a FUNCTION. As we covered before.
What is a return value? and why does it have to exist?
Let's answer that by writing a simple program.
Using a FUNCTION in Pascal
This time we'll write a simple program to find the result of an integer b raised to the power of an integer n.
[ hasil = b^n ]
Without a sub program (FUNCTION), the solution looks roughly like this.
program cariPangkat;
uses crt;
var
b, n: integer;
hasil: integer;
begin
clrscr;
n := 5; // the value of n=5
b := 2; // the value of b=2
hasil := 1;
// run a while loop
// to multiply the number b by itself n times
while n > 0 do
begin
n := n-1;
hasil := hasil * b;
end;
// output the result
writeln('hasil: ', hasil);
readln;
end.
That program computes the value of 25, which comes out as 32.
The method we use to find the power is to multiply the number b by itself n times using a loop.
That code isn't wrong, but what happens if you want to print several powers?
With the power of copy-paste, when you want the result of 25 and 52 the code turns into this.
program cariPangkat;
uses crt;
var
b, n: integer;
hasil: integer;
begin
clrscr;
n := 5; // the value of n=5
b := 2; // the value of b=2
hasil := 1;
// run a while loop
// to multiply the result by b n times
while n > 0 do
begin
n := n-1;
hasil := hasil *b;
end;
// output the result
writeln('hasil: ', hasil);
n := 2; // the value of n=2
b := 5; // the value of b=5
hasil := 1;
// run a while loop
// to multiply the result by b n times
while n > 0 do
begin
n := n-1;
hasil := hasil *b;
end;
// output the result
writeln('hasil: ', hasil);
readln;
end.
The code ends up long, tangled, and unpleasant to look at.
This is where FUNCTION comes in. Let's reformat that code using a sub program in the form of a FUNCTION.
program cariPangkat;
uses crt;
var
hasil: integer;
function hitungPangkat(b, n: integer): integer;
var
h: integer;
begin
h := 1;
while n > 0 do
begin
n := n-1;
h := h*b;
end;
hitungPangkat := h;
end;
begin
clrscr;
// compute 2^5
hasil := hitungPangkat(2, 5);
writeln('hasil: ', hasil);
// compute 5^2
hasil := hitungPangkat(5, 2);
writeln('hasil: ', hasil);
readln;
end.
Now run that program. It will come out with the same output as before.
In that program the FUNCTION hitungPangkat has the same logic as our earlier code, using a loop to multiply the number b by itself n times.
The result of that repeated multiplication goes into the local variable h. Once the loop finishes, the value of h is assigned to the FUNCTION name, hitungPangkat. That FUNCTION name acts as if it were a variable of type integer (the return type).
Then, in the main program, we call the FUNCTION and assign the result to the variable hasil. And the output comes out as intended.
To make it even simpler, we can drop the variable hasil and call the FUNCTION hitungPangkat directly as a parameter in the PRINTLN command.
program cariPangkat;
uses crt;
function hitungPangkat(b, n: integer): integer;
var
h: integer;
begin
h := 1;
while n > 0 do
begin
n := n-1;
h := h*b;
end;
hitungPangkat := h;
end;
begin
clrscr;
writeln('hasil: ', hitungPangkat(2, 5));
writeln('hasil: ', hitungPangkat(5, 2));
writeln('hasil: ', hitungPangkat(5, 5));
writeln('hasil: ', hitungPangkat(2, 2));
readln;
end.
Wrapping Up
That's enough for this time, hope it helps :D
Next time we'll cover FUNCTION and PROCEDURE that call themselves, or what we usually call the concept of recursion in Pascal.