Home » Developer & Programmer » Forms » what is wrong with this code?  () 1 Vote
what is wrong with this code? [message #202261] Wed, 08 November 2006 21:50 Go to next message
qewani
Messages: 51
Registered: December 2005
Location: uaq
Member

here is my code
function COMPARISON (in1 number, in2 number) is
if in1 = in2 then
return(1);
else 
return(0);
end if;
end;

if i compile it it give me error saying that encountered the symbol "IF" when excepting one of the following:
Fatal syntax error, unable to recover.

Please help!
Re: what is wrong with this code? [message #202265 is a reply to message #202261] Wed, 08 November 2006 22:02 Go to previous messageGo to next message
djmartin
Messages: 10180
Registered: March 2005
Location: Canberra ACT Australia
Senior Member
Account Moderator
You have to 'declare' the return value.

If you had fed your code through this site's formatter you would have received this message "Error Messages is(1,46) expected token:RETURN".

David
Re: what is wrong with this code? [message #202269 is a reply to message #202265] Wed, 08 November 2006 22:09 Go to previous messageGo to next message
qewani
Messages: 51
Registered: December 2005
Location: uaq
Member

ok thanks David
Re: what is wrong with this code? [message #202321 is a reply to message #202269] Thu, 09 November 2006 02:11 Go to previous messageGo to next message
Littlefoot
Messages: 20901
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
'Declare' return value doesn't, actually, mean that the value itself has to be declared in the DECLARE section. It means that FUNCTION has to have, oh well, declared return value datatype. Also, your function doesn't have the BEGIN keyword. Therefore, it might look like this:
CREATE OR REPLACE FUNCTION comparison (in1 NUMBER, in2 NUMBER)
   RETURN NUMBER
IS
BEGIN
   IF in1 = in2
   THEN
      RETURN (1);
   END IF;
   RETURN (0);
END;

However, you might also consider returning a BOOLEAN instead of a NUMBER:
CREATE OR REPLACE FUNCTION Cmp (in1 NUMBER, in2 NUMBER)
RETURN BOOLEAN
IS
BEGIN
  RETURN in1 = in2;
END;

Such a function is more meaningful, because your current function looks like this (for COMPARISON (2, 3)):

Q: Numbers 2 and 3 are equal
A: 0

If it was a Boolean, it would look like this:

Q: Numbers 2 and 3 are equal
A: FALSE
Re: what is wrong with this code? [message #202323 is a reply to message #202321] Thu, 09 November 2006 02:20 Go to previous messageGo to next message
qewani
Messages: 51
Registered: December 2005
Location: uaq
Member

Thanks. Cool
Re: what is wrong with this code? [message #202450 is a reply to message #202321] Thu, 09 November 2006 22:01 Go to previous messageGo to next message
djmartin
Messages: 10180
Registered: March 2005
Location: Canberra ACT Australia
Senior Member
Account Moderator
@Littlefoot,

Have you tried displaying a boolean in a message lately? Why would I want to do this? So I can debug my code. Boolean is nice but either character or number is easier to handle.

David
Re: what is wrong with this code? [message #202501 is a reply to message #202450] Fri, 10 November 2006 01:37 Go to previous messageGo to next message
Littlefoot
Messages: 20901
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
djmartin
Boolean is nice but either character or number is easier to handle.

./fa/1725/0/

[Updated on: Mon, 13 November 2006 00:29]

Report message to a moderator

Re: what is wrong with this code? [message #202798 is a reply to message #202501] Sun, 12 November 2006 21:11 Go to previous messageGo to next message
djmartin
Messages: 10180
Registered: March 2005
Location: Canberra ACT Australia
Senior Member
Account Moderator
Did you mean '/1725/'?

David
Re: what is wrong with this code? [message #202835 is a reply to message #202798] Mon, 13 November 2006 00:31 Go to previous message
Littlefoot
Messages: 20901
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Yes, I did Smile (it appears that when the message is deleted, the file gets deleted as well); I tested it on the wrong file, obviously.

Sorry!
Previous Topic: POPULATE LIST ELEMENT FROM A RECORD GROUP
Next Topic: How to declare a pl/sql table globally
Goto Forum:
  


Current Time: Fri Dec 09 15:50:01 CST 2016

Total time taken to generate the page: 0.11012 seconds