RECURSIVE PROCEDURE and FUNCTION

18 Apr

Procedures and functions is a sub program that is very useful in programming, especially for large programs or projects. Benefits of using sub-programs include:

Procedures and functions is a sub program that is very useful in programming, especially for large programs or projects. Benefits of using sub-programs include:

increase readibility, which facilitate the reading program
increase modularity, that is something that big break into modules or parts that are smaller in accordance with its function, thus simplifying the checking, testing and localization errors.
enhance reusability, which is a sub-program can be used repeatedly by simply calling the sub program without writing the commands should be repeated.

Sub Recursive Program is a sub program that calls itself as long as the conditions met callings.

Given the nature of the sub is a recursive program over the sub-recursive program must have:

condition that causes him to stop calling (called the special conditions or special conditions)
calling themselves the sub program (ie if specific conditions are not met)

In general form of recursive sub-programs have a conditional statement:

if special conditions not met
then call the self-righteous with the appropriate parameters
else do the instruction to be executed when the special conditions met

Sub recursive programs are generally used for problems that have a completion step, patterned or irregular steps. If we have a problem and we know the solution algorithm, sometimes sub recursive program of choice when it allows us to use. The algorithmic (in terms of algorithms, that is, if we consider the memory usage, execution time of sub program) sub recursive programs are often inefficient.
Thus the recursive sub-programs typically have efficiencies in the writing of orders, but sometimes not efficient algorithmic. Even so many problems that are better suited solved by a recursive manner (eg in the search / searching, which will be discussed at the meetings that will come).

Examples of recursive sub-programs in the Pascal language.

A simple example
PROCEDURE TULIS_1 (many: integer; word: string);
begin
if a lot of> 1 then TULIS_1 (lot-1, word);
Writeln (word, lots: 5);
end;

OUTPUT (eg called by TULIS_1 (5, “Print to”))

Matter to 1
Matter to 2
Matter to 3
Prints to 4
Matter to 5

Compare the above procedure and its output with the procedure below!

PROCEDURE TULIS_2 (many: integer; word: string);
begin
Writeln (word, lots: 5);
if a lot of> 1 then TULIS_1 (lot-1, word);
end;

OUTPUT (eg called by TULIS_2 (5, “Print to”))

Matter to 5
Prints to 4
Matter to 3
Matter to 2
Matter to 1

Why different results?

Applied Example
Mathematically, the multiplication of two positive integers a with b (written ab or axb) is essentially a summation of a tribe as much as b, ie a + a + a + …. B + a as much interest. Eg 2 x 3 can be interpreted as 2 + 2 + 2 = 6, ie the sum of 2 of 3 tribes Given that a number when multiplied by the number 1 (one) will produce a number itself, then multiplication problems to put it in the form of summation over can be easily solved by computer.

With non-recursive

With procedure

Procedure KALI_BIASA_P (a, b: integer; var result: longint);
var i: integer;
begin
result: = 0;
for i: = 1 to b do result: = result + a;
end;
With the function

Function KALI_BIASA_F (a, b: integer): longint;
var result: longint; i: integer;
begin
result: = 0;
for i: = 1 to b do result: = result + a;
KALI_BIASA_F: = result;

end;

With Recursive

By Procedure

Procedure KALI_REK_P (a, b: integer; var result: longint)
begin
if b> 1 then KALI_REK_P (a, b-1, result);
result: = result + a;
end;
By Function

Function KALI_REK_F (a, b: integer): longint;
begin
if b> 1 then
KALI_REK_F: = KALI_REK_F (a, b-1) + a
else
KALI_REK_F: = a;
end;

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: