| in, out parameters [message #643764] |
Sat, 17 October 2015 06:30  |
 |
wisdomofmetis
Messages: 2 Registered: October 2015 Location: istanbul
|
Junior Member |
|
|
Hello, i think i understand parameter modes in, out and in out wrongly. Here is what i understand :
in : read only parameter,this parameter cannot be written function cannot return this paramter.
out : write only parameter,this parameter cannot be read function can return this parameter.
Here is a quote from tutorials :
IN represents that value will be passed from outside and OUT represents that this parameter will be used to return a value outside of the procedure.
I understand from the above sentence that functions can only return out parameters, but not in parameters.
However by testing some codes, i observed that functions can return in parameters. Then what does this mean:"OUT represents that this parameter will be used to return a value outside of the procedure."
Also, in the below code, out variable is tried to be read, it cannot be read but it does not cause a compilation error :S
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x out number, y out number)
RETURN number
IS
z number;
BEGIN
dbms_output.put_line('x : ' || x);
dbms_output.put_line('y : ' || y);
IF x > y THEN
return x;
ELSE
return y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
/
Can please clarify my confusions?
thanks in advance.
[Updated on: Sat, 17 October 2015 06:43] Report message to a moderator
|
|
|
|
| Re: in, out parameters [message #643766 is a reply to message #643764] |
Sat, 17 October 2015 07:51   |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
Hello, it seems that you misunderstood the word "return". It does not mean the return value of the function (value passed from the function with the RETURN statement), but passing the value of the parameter itself (in the variable which was used in the function call) to the caller after the end of the procedure execution.
If you used "change" instead of "return" in the initial paragraph, it would make some sense. But, as read/write applies to passing of the parameter from/to the caller, the OUT can be used inside the procedure as well - it is just empty in the beginning regardless the passed value (it is not read in the beginning).
In your case, the parameters should be IN - you are passing their value from the caller (not possible when they are OUT) and their value does not change inside the function.
Something more in the documentation of 11gR2: http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/subprograms.htm#LNPLS00815
There are examples below on that page. As I do not have access to Oracle, I will not write any other example; anyway; can you reveal what the function returns? (I bet on NULL - nothing)
|
|
|
|
| Re: in, out parameters [message #643767 is a reply to message #643764] |
Sat, 17 October 2015 07:51  |
Solomon Yakobson
Messages: 3312 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
OK, your code compiled and executed without errors. But did you check returned value? Did you notice it is NULL no matter what? And did you think why? By declaring parameter as OUT you disabled passing parameter value to your function. OUT parameters are always initialized, so a and b will be set to NULL no matter what values you assign to a and b prior to calling function. Anyway, IN parameter value is passed to procedure/function but can't be changed. OUT parameter value can't be passed to procedure/function (OUT parameter is initialized so any value parameter has prior the call is lost). OUT parameter value can be set/changed and whatever value it is set to will be assigned back to actual parameter so calling routine will be able to use it.
SY.
|
|
|
|