How can I pass arguments from my procedure to a call of write called inside ?
Something quite like that:
procedure smth (args: alltypes);
begin
write(args);
end;
If you want to use your function with any number/type of argument in Write manner, like smth(3, 'aaa', 5.6) - it is impossible as i know. However you can use array of ... type for argument to pass to the procedure any number of arguments.
Here is an example:
program wrt;
{$mode objfpc}{$H+}
uses
sysutils, variants;
procedure test1(args: array of Variant);
var
i: Integer;
begin
for i := Low(args) to High(args) do
Write(args[i]);
Writeln;
end;
procedure test2(fmt: string; args: array of const);
begin
Writeln(Format(fmt, args));
end;
begin
test1([1, 'aaa', 3.5, False]);
test2('%d %s %g, %s', [1, 'aaa', 3.5, BoolToStr(False, True)]);
end.
For example:
procedure write( text : string );
begin
write( text );
end;
But if you want to override your function. You have to read that topic HERE. That will allow you to make function with more type of arguments.
Related
i am trying to call a function from stored procedure in Oracle, but not getting any idea how to do.
my function has two IN parameter and one OUT parameter.
in my procedure i am using out sys refcursor . Any refrence or example will help me a lot.
Here is a simple example for calling function inside procedure. Also as mentioned by APC using OUT in function is a bad practice. Instead you can return your required output. And I'm not sure how you are using sys_refcursor, so modify your procedure accordingly
CREATE OR REPLACE FUNCTION SUM_OF_2(NUM1 IN NUMBER,NUM2 IN NUMBER) RETURN NUMBER
IS
RESULT_SUM NUMBER;
BEGIN
RESULT_SUM:=NUM1+NUM2;
RETURN RESULT_SUM;
END;
CREATE OR REPLACE PROCEDURE CALL_FUNCTON(NUM1 NUMBER,NUM2 NUMBER)
AS
V_FINAL_RESULT NUMBER;
BEGIN
V_FINAL_RESULT:=SUM_OF_2(NUM1,NUM2);
DBMS_OUTPUT.PUT_LINE(V_FINAL_RESULT);
END;
BEGIN
CALL_FUNCTON(5,10);
END;
/
CHECK DEMO HERE
Not sure on what your requirement is , maybe you are just trying the code for education purposes. Generally I have not seen much code which uses OUT parameter with functions, in case you want to return multiple values to the caller object then you could use a procedure with more then one OUT variables. There are some limitation on how an oracle function with OUT parameter would differ from a normal function.
CREATE OR REPLACE FUNCTION temp_demo_func(out_var1 OUT NUMBER)
RETURN VARCHAR2 IS
BEGIN
out_var1 := 1;
RETURN 'T';
EXCEPTION
WHEN OTHERS THEN
RETURN 'F';
END temp_demo_func;
/
CREATE OR REPLACE PROCEDURE temp_demo_proc
(
in_var1 NUMBER
,cur_refcur_out OUT SYS_REFCURSOR
) IS
res VARCHAR2(1);
out_var1 NUMBER;
BEGIN
res := temp_demo_func(out_var1 => out_var1);
dbms_output.put_line(out_var1);
OPEN cur_refcur_out FOR
SELECT in_var1
,out_var1
,res
FROM dual;
END;
/
set serveroutput on
declare
cur_refcur_out Sys_Refcursor;
in_var1 number := 22;
begin
temp_demo_proc(in_var1 => in_var1
,cur_refcur_out => cur_refcur_out);
end;
/
I want square ,but i dont remember how to do it becauseI multiplying is a variable, but throw exeption and i don't know why.
please help i don't know what else to do
program Project1;
uses crt;
type TSquare=class
len:integer;
place:integer;
function Perimetr:integer;
function Area:integer;
function Verify():boolean;
procedure Show(P,S:real);
constructor Create(P,l:integer);
end;
function TSquare.Perimetr:integer;
var P:integer;
begin
P:=len*4;
end;
function TSquare.Area:integer;
var S:integer;
begin
S:=len*len;
end;
function TSquare.Verify:boolean;
begin
end;
procedure TSquare.Show(P,S:real);
begin
write('Площидь=',S,'Перимитр=',P);
end;
constructor TSquare.Create(p,l:integer);
begin
len:=l;
place:=p;
end;
var r: TSquare;
a,b:integer;
begin
r.Create(1,5);
r.Show(r.Perimetr(),r.Area());
end.
SIGSEGV means access of invalid memory. Here you do not create the TSquare object correctly.
r := TSquare.Create(1,5); is the correct way to create an instance of an object.
I am getting the following error message while running the below code. I am new to coding world of pl/sql (oracle) and I request your assistance for the same.
Code:
create or replace package learn is
function Area(i_rad NUMBER) return NUMBER;
function Area(i_length NUMBER, i_width NUMBER:=3) return NUMBER;
end;
/
Package body:
create or replace package body learn is
function Area(i_rad NUMBER) return NUMBER
is
v_pi NUMBER:=3.14;
v number:=to_number(i_rad);
begin
return v_pi * (i_rad ** 2);
end;
function Area(i_length NUMBER, i_width NUMBER:=3) return NUMBER
is
begin
return i_length * i_width;
end;
end learn;
Plsql block
declare
x number(2):=2;
y number(2):=5;
begin
DBMS_OUTPUT.put_line('Area (R=3):'||learn.Area(x));
DBMS_OUTPUT.put_line('Area (R=3):'||learn.Area(x,y));
end;
Error Message: too many declarations of 'AREA' match this call
That's because you have default value for the second parameter in your two param function. If you provide only param, the second function will assume the second value to be 3 and now there are two functions that can be called and hence the call failed.
I'd suggest you not to do this kind of overloading as it is not clear which function does what.
If you still want to do this, one way is to make the second param mandatory and pass null if you don't have any value to pass.
create or replace package learn is
function Area(i_rad NUMBER) return NUMBER;
function Area(i_length NUMBER, i_width NUMBER) return NUMBER;
end;
/
create or replace package body learn is
function Area(i_rad NUMBER) return NUMBER
is
v_pi NUMBER:=3.14;
v number:=to_number(i_rad);
begin
return v_pi * (i_rad ** 2);
end;
function Area(i_length NUMBER, i_width NUMBER) return NUMBER
is
begin
return i_length * nvl(i_width,3);
end;
end learn;
/
declare
x number(2):=2;
y number(2):=5;
begin
DBMS_OUTPUT.put_line('Area (R=3):'||learn.Area(x));
DBMS_OUTPUT.put_line('Area (R=3):'||learn.Area(x,y));
end;
/
If you have different param names, you can do this:
declare
x number(2):=2;
y number(2):=5;
begin
DBMS_OUTPUT.put_line('Area (R=3):'||learn.Area(i_rad => x));
DBMS_OUTPUT.put_line('Area (R=3):'||learn.Area(x,y));
end;
/
Since i_width has a default value, you have two functions that can be called with a single number argument. Since both these functions calculate different areas, a good way to differentiate would be to simply use different names:
CREATE OR REPLACE PACKAGE learn IS
FUNCTION circle_area(i_rad NUMBER) RETURN NUMBER;
FUNCTION rectangle_area(i_length NUMBER, i_width NUMBER:=3) RETURN NUMBER;
-- And the same changes in the package body, of course.
END;
/
I have the following PL/SQL code:
create type testingclob as object (
member_value number,
constructor function testingclob(
i_aclob clob
) return self as result
);
/
create type body testingclob as
constructor function testingclob(
i_aclob clob
) return self as result
is
begin
member_value := 0;
return;
end;
end;
/
declare
l_test testingclob;
begin
l_test := new testingclob('some text');
end;
But I get the error
ORA-06550: line 5, column 18:
PLS-00307: too many declarations of 'TESTINGCLOB' match this call
ORA-06550: line 5, column 4:
The compilation of the type works fine. However it appears that I cannot use the constructor. Does anybody know what I am doing wrong?
The parameter 'some text' should be declared as clob.
declare
l_param clob;
l_test testingclob;
begin
l_param:= 'some text';
l_test := new testingclob(l_param);
end;
By default, the system supplies a default constructor that accepts a parameter corresponding to each attribute, see https://docs.oracle.com/cd/B13789_01/appdev.101/b10807/10_objs.htm#i16312 chapter 'defining object constructors'. Therefore the constructor cannot be determined definitely because none of them hits the input parameter varchar2.
Try
begin
l_test := new testingclob(1);
end;
This is your default constructor.
I want to understand this code, especially PROCEDURE
PROGRAM vowels;
USES crt;
{Program that counts the number of vowels in a sentence}
CONST space=' ';
maxchar=80;
TYPE vowel=(a,e,i,o,u);
VAR buffer:ARRAY[1..maxchar] of char;
vowelcount:ARRAY[vowel] of integer;
PROCEDURE initialize;
VAR ch:vowel;
BEGIN
FOR ch:=a TO u DO
BEGIN
vowelcount[ch]:=0;
END;
END;
PROCEDURE textinput;
VAR index:integer;
BEGIN
writeln('Input a sentence');
FOR index:=1 TO maxchar DO
IF eoln THEN buffer[index]:=space
ELSE read(buffer[index]);
readln;
END;
PROCEDURE analysis;
VAR index:integer;
ch:vowel;
BEGIN
index:=1;
WHILE index<>maxchar+1 DO
BEGIN
IF buffer[index] IN ['a','e','i','o','u'] THEN
BEGIN
CASE buffer[index] OF
'a':ch:=a;
'e':ch:=e;
'i':ch:=i;
'o':ch:=o;
'u':ch:=u;
END;
vowelcount[ch]:=vowelcount[ch]+1;
END;
index:=index+1;
END;
END;
PROCEDURE vowelout;
VAR ch:vowel;
BEGIN
clrscr;
writeln;
writeln(' a e i o u');
FOR ch:=a TO u DO
write(vowelcount[ch]:4);
writeln;
END;
BEGIN
initialize;
textinput;
analysis;
vowelout;
END;
Overall: Okay this code is counting the number of vowels supplied in the input string.
Lets Begin....
TYPE vowel=(a,e,i,o,u); VAR
buffer:ARRAY[1..maxchar] of char;
vowelcount:ARRAY[vowel] of integer;
This code is defining a list of the vowels in english (a,e,i,o,u).
PROCEDURE initialize; VAR ch:vowel;
BEGIN FOR ch:=a TO u DO BEGIN
vowelcount[ch]:=0; END; END;
It then defines a variable to collect the number of each vowel, called vowelcount. That variable is an array, looks sort of like this:
vowelcount[a]=0;
vowelcount[e]=0;
vowelcount[i]=0; #... etc
Then the procedure "Analysis" is defined. This takes the input from the screen (which will be called later on in the program) and steps through each letter in the input.
WHILE index<>maxchar+1 DO BEGIN IF
buffer[index] IN ['a','e','i','o','u']
THEN BEGIN CASE buffer[index] OF
'a':ch:=a; 'e':ch:=e; 'i':ch:=i;
'o':ch:=o; 'u':ch:=u; END;
If any of those letters happens to be in the list of letters than matches a vowel, then it will add one to the number in the vowelcount array above. (vowelcount[ch]:=vowelcount[ch]+1) where ch is the matched letter. As you can see this is only triggered if it is a valid vowel (IF buffer[index] IN ['a','e','i','o','u'] )
Finally. The main code of the program, or what is actually run:
BEGIN clrscr; writeln; writeln(' a e i
o u'); FOR ch:=a TO u DO
write(vowelcount[ch]:4); writeln; END;
BEGIN initialize; textinput; analysis;
vowelout; END.
This basically strings the application together, starting by clearing the screen (in a dos prompt) and then outputting the vowels onto the screen. It then adds some formatting and outputs the current count of vowelcount (as above).
It will then request your input and finally it will output the contents of vowelcount again, which has been updated with the vowelcounts from the input you made.