# Recursive programming…

If you don’t know where to start your with your program, recursion might be an option. I am working on some (perhaps useless) package code to convert an integer into a boolean representation. When I am converting the integer, I don’t know where to start my conversion. If the integer is relatively small and I start with a high bit (too big) then the overhead is relatively big.

If I start with a smaller bit, I might be missing stuff. Or I will have to do a lot of checking in my code. I always want my computer (or the database) to do the work for me, with as little code as possible. So I want the program to figure out where to start. All I would have to do is provide the starting points and a parameter to catch the return value.

By using recursion in this situation, I can make sure that I will never miss a thing. The idea is checking whether a power of 2 is bigger than the value being converted. If this value is smaller than the to-be-converted-value (tbcv), call the same procedure but with a higher number for the power calculation. When this value is bigger, then add a 0 to the returning string (this will be removed later on). Now, control is being returned to the previous procedure in the call stack. Now we have to check if it’s possible to subtract the current value from the tbcv without the result becoming below 0. If this is possible, then a 1 is added to the returning string. If it is not possible, then a 0 is added to the returning string. After this control is passed back to the previous procedure in the call stack. And so forth and so on.

FUNCTION BINARY(number_in IN PLS_INTEGER
, positions_in IN PLS_INTEGER DEFAULT NULL )
RETURN VARCHAR2
IS
l_returnvalue bar\$util.maxvarchar;
FUNCTION parseDecimalToBoolean(decimal_in IN VARCHAR2) RETURN VARCHAR2
IS
l_decimal_in PLS_INTEGER;
l_returnvalue bar\$util.maxvarchar;
PROCEDURE addBooleanChar(decimal_in_out IN OUT NOCOPY PLS_INTEGER
, power_in IN PLS_INTEGER
, booleanchar_in_out IN OUT NOCOPY VARCHAR2)
IS
l_powered PLS_INTEGER;
BEGIN
l_powered := power(2, power_in);
IF l_powered < decimal_in_out THEN
END IF;
IF l_powered > decimal_in_out THEN
booleanchar_in_out := booleanchar_in_out || ‘0’;
ELSE
booleanchar_in_out := booleanchar_in_out || ‘1’;
decimal_in_out := decimal_in_out – l_powered;
END IF;
BEGIN
l_returnvalue := ”;
l_decimal_in := decimal_in;
— remove any leading zero’s (if they’re there)
l_returnvalue := ltrim(l_returnvalue, ‘0’);
RETURN l_returnvalue;
END parseDecimalToBoolean;
BEGIN
IF number_in IS NULL THEN
l_returnvalue := NULL;
ELSE
IF number_in = 0 THEN
l_returnvalue := ‘0’;
ELSE
l_returnvalue := parsedecimaltoboolean(number_in);
END IF;
IF positions_in IS NOT NULL THEN