• Functions are similar to procedures but with a key
difference: functions return a single value, whereas procedures do not return any value directly. • Functions are used to perform computations and return results to the calling program or procedure. • They can accept parameters, just like procedures, and can be called from SQL statements or other PL/SQL blocks. CREATE [OR REPLACE] FUNCTION function_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] RETURN return_datatype {IS | AS} BEGIN < function_body > END [function_name]; | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | +----+----------+-----+-----------+----------+ CREATE OR REPLACE FUNCTION totalCustomers RETURN number IS total number(2) := 0; BEGIN SELECT count(*) into total FROM customers;
RETURN total; END; / FUNCTION CALL
• To use a function, you will have to call that function to
perform the defined task. • When a program calls a function, the program control is transferred to the called function. • A called function performs the defined task and when its return statement is executed or when the last end statement is reached, it returns the program control back to the main program. DECLARE c number(2); BEGIN c := totalCustomers(); dbms_output.put_line('Total no. of Customers: ' || c); END; / RESULT:Total no. of Customers: 6
PL/SQL procedure successfully completed.
PL/SQL 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. • calculate the factorial of a number. n! = n*(n-1)! THANKYOU
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More