1

I'm trying to create a function to calculate N-th element of Fibonacci sequence.

CREATE OR REPLACE FUNCTION fib_mohamed_h(x number)
RETURN number IS
  f number;
DECLARE
  num number;
  fibbonnacci number
BEGIN
  IF (x<3) THEN
    f := 1;
  ELSE
    f := fib_mohamed_h(x-1)+fib_mohamed_h(x-2);
  END IF;
  RETURN f;
  x:= 3;
  fibbonnacci := fib_mohamed_h(x);
  dbms_output.put_line(' Fibbonnacci '|| x || ' is ' || fibbonnacci);
END;

This gets:

Warning: execution completed with warning FUNCTION fib_mohamed_h(x Compiled.

What am I doing wrong?

Theolodis
  • 4,977
  • 3
  • 34
  • 53
user3231655
  • 75
  • 2
  • 11
  • 1
    Please add the **complete** stored procedure –  Feb 18 '15 at 12:05
  • And the actual error; not sure which client this is, but you can query the `user_errors` view to get the details. – Alex Poole Feb 18 '15 at 12:10
  • I am a newbie can any help me in function which allows to calculate N-th element of Fibonacci sequence in plsql – user3231655 Feb 18 '15 at 12:16
  • The problem isn't with your logic, it's with how you are creating the function, I think. Please edit your question to show the **complete** function creation statement, not just that little snippet from the middle of it. (The 'Compiled' message makes it look like you've used double-quotes incorrectly in the create statement, but only showing the whole thing and the error messages from `user_errors` will make the problems clear). – Alex Poole Feb 18 '15 at 12:22
  • I am not able to edit question here is the code – user3231655 Feb 18 '15 at 12:27
  • CREATE OR REPLACE FUNCTION fib_mohamed_h(x number) RETURN number IS f number; DECLARE num number; fibbonnacci number;BEGIN IF (x<3) THEN f := 1; ELSE f := fib_mohamed_h(x-1)+fib_mohamed_h(x-2); END IF; RETURN f; x:= 3; fibbonnacci := fib_mohamed_h(x); dbms_output.put_line(' Fibbonnacci '|| x || ' is ' || fibbonnacci); END; – user3231655 Feb 18 '15 at 12:28
  • There is an edit button right under the question text. I've copied that in for you this time, and formatted it so it's readable. But what you supplied is a mix of a function and an anonymous block - you seem to have everything mixed together, is that really what you're executing? And what is in `user_errors`? – Alex Poole Feb 18 '15 at 12:48
  • 1
    By the way, this part: `IF (x<3) THEN` is not correct. The Fibonacci function should return 0 for 0 and there are values for negative numbers as well. If you only want to return Fibonacci numbers for the natural numbers then you should raise an exception when your function is called with numbers 0 or less. – David Faber Feb 18 '15 at 15:09
  • You might also consider an iterative approach or even a lookup table http://stackoverflow.com/questions/9122277/what-is-a-non-recursive-solution-for-fibonacci-like-sequence-in-java – David Faber Feb 18 '15 at 15:12

1 Answers1

4

You seem to have merged the function declaration and the call to the function into one invalid block. It looks almost like a cut-and-paste error. With your logic the function should just be:

CREATE OR REPLACE FUNCTION fib_mohamed_h(x number)
RETURN number IS
 f number;
BEGIN
  IF (x<3) THEN
    f := 1;
  ELSE
    f := fib_mohamed_h(x-1)+fib_mohamed_h(x-2);
  END IF;
  RETURN f;
END;
/

And the call might be:

set serveroutput on size unlimited
DECLARE
  x number;
  fibbonnacci number;
BEGIN
  x:= 3;
  fibbonnacci := fib_mohamed_h(x);
  dbms_output.put_line(' Fibbonnacci '|| x || ' is ' || fibbonnacci);
END;
/

anonymous block completed
 Fibbonnacci 3 is 2

Or from plain SQL:

SELECT fib_mohamed_h(7) FROM dual;

FIB_MOHAMED_H(7)
----------------
              13 

SQL Fiddle demo.

Read more about how to create functions.

Alex Poole
  • 183,384
  • 11
  • 179
  • 318