BUG #14184: Function is running correct but not showing output

Started by zubair alamalmost 10 years ago4 messagesbugs
Jump to latest
#1zubair alam
zzia88@gmail.com

The following bug has been logged on the website:

Bug reference: 14184
Logged by: Zubair Alam
Email address: zzia88@gmail.com
PostgreSQL version: 9.5.3
Operating system: windows7-x64(64 bit)
Description:

postgresql-9.5.3-1-windows-x64
-----<<<<<<My function Coding>>>>----
CREATE OR REPLACE FUNCTION FACTORIAL(IN NUM BIGINT,OUT FACT BIGINT) AS $$
DECLARE
FACT BIGINT;
BEGIN
FOR I IN REVERSE 1..NUM LOOP
FACT:=FACT*I;
END LOOP;
END;
$$ LANGUAGE plpgsql;
-----<<<<<<<<<<<<>>>>>>>>>>>>>-----
SELECT* FROM FACTORIAL(5);

IT SHOWING NULL VALUE...

--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

#2Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: zubair alam (#1)
Re: BUG #14184: Function is running correct but not showing output

On Mon, Jun 13, 2016 at 8:40 AM, <zzia88@gmail.com> wrote:

-----<<<<<<My function Coding>>>>----
CREATE OR REPLACE FUNCTION FACTORIAL(IN NUM BIGINT,OUT FACT BIGINT) AS $$
DECLARE
FACT BIGINT;
BEGIN
FOR I IN REVERSE 1..NUM LOOP
FACT:=FACT*I;
END LOOP;
END;
$$ LANGUAGE plpgsql;
-----<<<<<<<<<<<<>>>>>>>>>>>>>-----
SELECT* FROM FACTORIAL(5);

IT SHOWING NULL VALUE...

FACT is NULL on declaration. NULL (unknown) multiplied by anything is NULL.

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: zubair alam (#1)
Re: BUG #14184: Function is running correct but not showing output

zzia88@gmail.com writes:

CREATE OR REPLACE FUNCTION FACTORIAL(IN NUM BIGINT,OUT FACT BIGINT) AS $$
DECLARE
FACT BIGINT;
BEGIN
FOR I IN REVERSE 1..NUM LOOP
FACT:=FACT*I;
END LOOP;
END;
$$ LANGUAGE plpgsql;

SELECT* FROM FACTORIAL(5);

IT SHOWING NULL VALUE...

Yes. You didn't initialize FACT to something non-null (like, say, 1)
and multiplying null by anything will produce another null.

I think the REVERSE in your loop is wrong, too.

regards, tom lane

--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: zubair alam (#1)
Re: BUG #14184: Function is running correct but not showing output

zubair alam <zzia88@gmail.com> writes:

CREATE OR REPLACE FUNCTION FACTORIAL(IN NUM BIGINT,OUT FACT BIGINT) AS $$
DECLARE
FACT BIGINT:=1;
BEGIN
FOR I IN REVERSE 1..NUM LOOP
FACT:=FACT*I;
END LOOP;
END;
$$ LANGUAGE plpgsql;

if i am doing FACT BIGINT:=1; the also it giving the output NULL;

Well, the *third* bug in your function is that you're re-declaring
FACT as a local variable, thereby masking the output parameter.

regards, tom lane

--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs