When I tried writing to an read-only parameter(IN) of a function, Oracle complains with an error. But that is not the case when reading from an write-only(OUT) parameter of a function. Oracle silently allows this without any error. What is the reason for this behaviour?.
The following code executes without any assignment happening to “so” variable:
create or replace function foo(a OUT number) return number
is
so number;
begin
so := a; --no assignment happens here
a := 42;
dbms_output.put_line('HiYA there');
dbms_output.put_line('VAlue:' || so);
return 5;
end;
/
declare
somevar number;
a number := 6;
begin
dbms_output.put_line('Before a:'|| a);
somevar := foo(a);
dbms_output.put_line('After a:' || a);
end;
/
Here’s the output I got:
Before a:6
HiYA there
VAlue:
After a:42
Reading from an OUT parameter is allowed: you could have written things in your OUT parameter at the beginning of a procedure and you might want to read the value it contains before returning, this is not a bug.
What happens here is that since it is an OUT parameter and not an IN OUT parameter, the value of
ais not passed to the functionfoo, so at the beginning of the procedure the OUT parameteracontains theNULLvalue. You can check this by commenting out the linea := 42;: