PL/SQL

Hands On No. 3 : User defined function in PL/SQL

About Function

A stored function (also called a user function or user-defined function) is a set of PL/SQL statements you can call by name. Stored functions are very similar to procedures, except that a function returns a value to the environment in which it is called. User functions can be used as part of a SQL expression.

CREATE [OR REPLACE] FUNCTION function_name (parameter_list)
RETURN return_type
IS [Declaration Section]
BEGIN [Executable Section]
[EXCEPTION]
[Exception Handling Section]
END;
                                    
Create user defined function in PL/SQL
CREATE OR REPLACE FUNCTION FULLNAME_DISPLAY ( 
    FNAME IN  VARCHAR2 , 
    LNAME IN  VARCHAR2  
) RETURN VARCHAR2 
IS 
     
BEGIN 
   RETURN (FNAME|| ' ' ||LNAME); 
END FULLNAME_DISPLAY; 
                                    
Exercise

P - 1 : Create a function to calculate the addition,subtraction, multiplication,division of two numbers.
P - 2 : Create a function to calculate sum of first n numbers.n will be entered by the user.