Adarsh D
Adarsh D

Reputation: 581

Cannot call function which is defined in a PL/SQL package

I have this PL/SQL package:

create or replace package palindrome as
    function check_palindrome(num int) return int;
end palindrome;


create or replace package body palindrome as
    function check_palindrome(num int) return int as 
        ans int;
        z int;
        r int;
        rev int;
    begin
        z := num;

        while z > 0 loop
            r := mod(z,10);
            rev := rev*10+r;
            z := floor(z/10);
        end loop;

        if rev=num then
            dbms_output.put_line('the no '||num ||' is a palindrome ');
        else
             dbms_output.put_line('the no '||num ||' is not a palindrome ');
        end if;     
     end check_palindrome;
end palindrome;

I created the above package which has one function check_palindrome(), but when I try to call the function using

begin
    palindrome.check_palindrome(343);
end;

I get this error

Error report -
ORA-06550: line 2, column 5:
PLS-00221: 'CHECK_PALINDROME' is not a procedure or is undefined
ORA-06550: line 2, column 5:
PL/SQL: Statement ignored

Why am I getting this error? The package body is compiled successfully but I am getting this error while calling the function.

Upvotes: 0

Views: 2015

Answers (2)

APC
APC

Reputation: 146179

Your program doesn't actually return anything and there seems no value in correcting that as you're just displaying the outcome to the screen. Instead you should turn it into a procedure:

create or replace package palindrome as
    procedure check_palindrome(num int) ;
end palindrome;


create or replace package body palindrome as
    procedure check_palindrome(num int)  as 
        ans int;
        z int;
        r int;
        rev int;
    begin
        z := num;

        while z > 0 loop
            r := mod(z,10);
            rev := rev*10+r;
            z := floor(z/10);
        end loop;

        if rev=num then
            dbms_output.put_line('the no '||num ||' is a palindrome ');
        else
             dbms_output.put_line('the no '||num ||' is not a palindrome ');
        end if;     
     end check_palindrome;
end palindrome;

Then you can call it successfully:

begin
    palindrome.check_palindrome(343);
end;

Upvotes: 1

TenG
TenG

Reputation: 4004

You've declared a FUNCTION, which RETURNS a value, but you've called it like you would a PROCEDURE, so you need:

DECLARE
  the_val INT;
BEGIN
  the_val := PALINDROME.check_palindrome(343);
END;
/

Upvotes: 4

Related Questions