I have to implement the classic number swap program using functions in PL/SQL (Receive two number values from user and display them swapped). However, I don't seem to be able to return two numbers using functions in PL/SQL. I wanted to know if there's any way I can return two values from a function in PL/SQL or if there's some other way to write this code altogether?
SET SERVEROUTPUT ON;
DECLARE
a NUMBER;
b NUMBER;
FUNCTION numSwap(num1 IN OUT NUMBER ,num2 IN OUT NUMBER )IS
temp_num NUMBER;
BEGIN
temp_num := num1;
num1 := num2;
num2 := temp;
END;
BEGIN
a := &a;
b := &b;
DBMS_OUTPUT.PUT_LINE('First Number = ' || a);
DBMS_OUTPUT.PUT_LINE('Second Number = ' || b);
--After swapping values
DBMS_OUTPUT.PUT_LINE('After swapping the values');
numSwap(a,b);
--Displaying the results
DBMS_OUTPUT.PUT_LINE('First Number = ' || a);
DBMS_OUTPUT.PUT_LINE('Second Number = ' || b);
END;
EDIT: To clarify, I realize that this is more or less the syntax for a procedure, But I wanted to know if there's any way to execute this same program using Functions
FUNCTION
toPROCEDURE
. The new values are returned through theIN OUT
parameters.FUNCTION
toPROCEDURE
" is not a legitimate answer.numSwap(a,b);
within a PL/SQL block.