Lecture 3.2 - Subprogram - Function
Lecture 3.2 - Subprogram - Function
Lecture 3.2 - Subprogram - Function
RETURN tot;
END;
/
Calling a Function
• To call a function, you simply need to pass the required parameters
along with the function name and if the function returns a value, then
you can store the returned value.
• Following program calls the function totalEmp from an anonymous
block −
DECLARE
c number(2);
BEGIN
c := totalEmp();
dbms_output.put_line('Total no. of Employees: ' || c);
END;
/
Example-2
DECLARE
z number;
BEGIN
ELSE Z:= y;
END IF;
RETURN z;
END;
BEGIN
END;
/
Recursive Functions
• We have seen that a program or subprogram may call another subprogram.
• When a subprogram calls itself, it is referred to as a recursive call and the
process is known as recursion.
n! = n*(n-1)!
= n*(n-1)*(n-2)!
...
= n*(n-1)*(n-2)*(n-3)... 1
Example Prg:-
DECLARE
n number;
f number;
FUNCTION fact(x number) RETURN number IS --definition of function
f number;
BEGIN
IF x=0 THEN f := 1;
ELSE f := x * fact(x-1);
END IF;
RETURN f;
END; --end of function
BEGIN
n:= 6;
f := fact(n); --calling the functions
dbms_output.put_line(' Factorial '|| n || ' is ' || f);
END; /
Advantages of Stored Procedure/Functions
Following are some advantages of stored procedure and function in
PL/SQL:
1. Improves Database Performance
• Compilation is automatically done by oracle engine.
• Whenever the calling of procedure or function is done ,the oracle engine
loads the compiled code into a memory area called System Global
Area(SGA) due to which execution becomes faster.
2. Provides Reusability and avoids redundancy
• The same block of code for procedure or function can be called any
number of times for working on multiple data.
• Due to which number of lines of code cannot be written repeatedly.
Advantages of Stored Procedure/Functions
3. Maintains Integrity
• Integrity means accuracy. Use of procedure or function ensures integrity
because they are stored as database objects by the oracle engine.
4. Maintains Security
• Use of stored procedure or function helps in maintaining the security of
the database as access to them and their usage can be controlled by
granting access/permission to users while the permission to change or to
edit or to manipulate the database may not be granted to users.
5. Saves Memory
• Stored procedure or function have shared memory. Due to which it saves
memory as a single copy of either a procedure or a function can be loaded
for execution by any number of users who have access permission.
References
• RamezElmasri and Shamkant B. Navathe, “Fundamentals of Database System”, The
Benjamin / Cummings Publishing Co.
• Korth and Silberschatz Abraham, “Database System Concepts”, McGraw Hall.
• C.J.Date, “An Introduction to Database Systems”, Addison Wesley.
• Thomas M. Connolly, Carolyn & E. Begg, “Database Systems: A Practical Approach
to Design, Implementation and Management”, 5/E, University of Paisley, Addison-
Wesley.
17
THANK YOU
For queries
Email: [email protected]
18