CREATE FUNCTION Fact (@parameter datatype)
RETURNS returndatatype
AS
BEGIN
--sql statements
RETURN --whatever you want to return but of returndatatype from above
END
//the syntax to create a function with input parameters:
CREATE FUNCTION function_name(input_parameter)
RETURNS return_type
AS
BEGIN
DECLARE
--Local Variables to be declared--
--SQL statements--
RETURN return_value
END
//example :
CREATE FUNCTION [dbo].Circle(@Radius int)
RETURNS real
AS
BEGIN
DECLARE
@Area real
SET @Area=3.14*@Radius*@Radius
RETURN @Area
END
// We will execute the function with the SELECT statement as:
SELECT [dbo].Circle(5) AS Area
CREATE [OR ALTER] FUNCTION <FNAME> (@<PARAMETER NAME1><DATATYPE>[SIZE]
RETURNS <return PARAMETER / ATTRIBUTE /VARIABLE DATATYPE>
AS
BEGIN
<FUNCTION BODY / STATEMENTS>
RETURN <return PARAMETER/ATTRIBUTE/VARIABLE NAME>
END