0

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

6
  • 1
    Just change FUNCTION to PROCEDURE. The new values are returned through the IN OUT parameters. Commented Sep 6, 2017 at 11:55
  • Is there any way to do it through a Function though? I realized that's probably proper syntax for a procedure but I was hoping I could somehow make it work for a Function as well Commented Sep 6, 2017 at 12:01
  • @TonyAndrews - It is not clear how the OP would use this function (and this is almost surely homework, so perhaps he won't use it at all). With that said, there is a huge difference between functions and procedures. A function can be called in various clauses of a query (SELECT, WHERE, GROUP BY...), and it can generally be used in expressions. A procedure can't. So "change FUNCTION to PROCEDURE" is not a legitimate answer.
    – user5683823
    Commented Sep 6, 2017 at 12:40
  • @mathguy I disagree: the OP has shown how it will be used: numSwap(a,b); within a PL/SQL block. Commented Sep 6, 2017 at 12:42
  • 1
    @SaloniMude - I asked myself the same question when I first started learning to program in C. In all languages, a function returns only one value; if you need to return two values, you need the equivalent of "structures". The answer you selected as "Correct" is exactly the right answer, in any programming language, not only in PL/SQL. Each language has its own concept of "structure", but the general principle is the same.
    – user5683823
    Commented Sep 6, 2017 at 12:43

3 Answers 3

1

Elaborating on MT0's example...

A function returns only one value. You could solve it by using a record (or possibly a collection) that holds two values. Please note that the record type is only declared inside the scope of the PLSQL block.

declare

   a number;
   b number;
   type two_numbers is record(
      a number,
      b number);

  l_two_numbers two_numbers;

   function swap(p_two_numbers two_numbers) return two_numbers is
    l_return two_numbers;
   begin
      l_return.a:=p_two_numbers.b;
      l_return.b:=p_two_numbers.a;

      return l_return;
   end;

begin
   a := &a;
   b := &b;


   l_two_numbers.a:=a;
   l_two_numbers.b:=b;

   dbms_output.put_line('First Number = ' || l_two_numbers.a);
   dbms_output.put_line('Second Number = ' || l_two_numbers.b);

   l_two_numbers := swap(l_two_numbers);

   dbms_output.put_line('First Number = ' || l_two_numbers.a);
   dbms_output.put_line('Second Number = ' || l_two_numbers.b);
end;
0
0

Use a procedure:

DECLARE 
  a NUMBER;
  b NUMBER;
  PROCEDURE numSwap(num1 IN OUT NUMBER ,num2 IN OUT NUMBER )
  IS 
    temp_num NUMBER := num1;
  BEGIN
    num1 := num2;
    num2 := temp_num;
  END;
BEGIN
  a := &a;
  b := &b;

  DBMS_OUTPUT.PUT_LINE('First Number = ' || a);
  DBMS_OUTPUT.PUT_LINE('Second Number = ' || b);

  numSwap(a,b);

  DBMS_OUTPUT.PUT_LINE('First Number = ' || a);
  DBMS_OUTPUT.PUT_LINE('Second Number = ' || b);
END;
/
3
  • Yup, I figured out how to do it through the procedure but I couldn't figure out how to do it using a Function. I was wondering if there's any way to make that code work for a function as well. Commented Sep 6, 2017 at 12:05
  • It is not clear how the OP would use this function (and this is almost surely homework, so perhaps he won't use it at all). With that said, there is a huge difference between functions and procedures. A function can be called in various clauses of a query (SELECT, WHERE, GROUP BY...), and it can generally be used in expressions. A procedure can't. So "change FUNCTION to PROCEDURE" is not a legitimate answer.
    – user5683823
    Commented Sep 6, 2017 at 12:40
  • 1
    @mathguy This answer directly references the OP's code where they define the function in a PL/SQL anonymous block and calls it in that PL/SQL scope; so there is no expectation that it is being used in the SQL scope (and cannot be used like that in Oracle 11 or earlier given how it is defined). Given that the OP's code has no RETURN clause (either in the function specification or body) and the function result is not assigned to a variable is instead written as per the PROCEDURE syntax then the simplest solution is to say you are using a procedure.
    – MT0
    Commented Sep 6, 2017 at 12:54
0

You can use an oracle type if you want to still call the a function (if you need to use in SELECT: Try the below

set serveroutput on

create or replace type t_swap
is object(first_number number, second_number number);
/

create or replace
function f_num_swap(p_first_number in number, p_second_number in number) return t_swap
is
  v_swap t_swap;
begin
  v_swap := t_swap(p_second_number, p_first_number);
  return v_swap;
end;
/

-- You can access from select
select f_num_swap(1, 2).first_number, f_num_swap(1, 2).second_number from dual;

-- Or you can access from PL/SQL block
declare
  v_swap t_swap;
begin
  v_swap := f_num_swap(1, 2);
  dbms_output.put_line('First Nubmer Swapped: '||v_swap.first_number);
  dbms_output.put_line('Second Nubmer Swapped: '||v_swap.second_number);
end;
/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.