A more elegant/flexible solution to sum five numbers [message #582101] |
Sat, 13 April 2013 07:08 |
|
Kimberlicious
Messages: 10 Registered: April 2013
|
Junior Member |
|
|
Hey, I am creating a function to sum five numbers (less 1).
Basically, I think my method of solving this problem is not very flexible, is it possible to have an array of numbers in an SQL function, and how would this be implemented? (Im sorry, I am very new to all this and haven`t really got a clue).
Here is the screenshot of my output (I cannot embed links until 5 posts!): flic.kr/p/eaSHBP
And my code:
CREATE OR REPLACE FUNCTION sumfivenumbers ( n1 NUMBER, n2 NUMBER, n3 NUMBER, n4 NUMBER, n5 NUMBER)
RETURN NUMBER
IS
Sumnums NUMBER;
BEGIN
SELECT SUM(n1+n2+n3+n4+n5-1) INTO Sumnums FROM DUAL;
DBMS_OUTPUT.PUT_LINE(Sumnums);
RETURN 1;
END sumfivenumbers;
/
SELECT sumfivenumbers(5,5,5,5,5) AS "Five Numbers less 1" FROM DUAL;
Any tips or suggestions you may have would be greatly appreciated
|
|
|
|
|
|
|
|
|
Re: A more elegant/flexible solution to sum five numbers [message #582137 is a reply to message #582135] |
Sat, 13 April 2013 20:11 |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
Sounds like a class exercise. It's not uncommon for teachers to set a simple and perhaps meaningless assignment (summing 5 numbers less 1) to practice a non-trivial skill (create a function with arguments).
If I'm right, then I wouldn't expect extra credit for using features like nested tables that are completely out of scope of the assignment. I'd be looking to dress it up with an optional decrement parameter (in case you want to subtract something other than 1), making it DETERMINISTIC and PARALLEL and enabling the RESULT_CACHE.
Ross Leishman
|
|
|