Content deleted Content added
m Reverted edits by 113.19.83.250 (talk): Unexplained removal of content (HG) (3.1.20) |
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5 |
||
(32 intermediate revisions by 25 users not shown) | |||
Line 1:
{{Short description|Function provided by the user of a program or environment}}
A '''user-defined function''' ('''UDF''') is a [[function (programming)|function]] provided by the user of a program or environment, in a context where the usual assumption is that functions are built into the program or environment. UDFs are usually written for the requirement of its creator. ==BASIC language==
In some old implementations of the [[BASIC]] programming language, user-defined functions are defined using the "DEF FN" syntax. More modern dialects of BASIC are influenced by the [[structured programming]] paradigm, where most or all of the code is written as user-defined functions or procedures, and the concept becomes practically redundant.
==COBOL language==
In the [[COBOL]] programming language, a user-defined function is an entity that is defined by the user by specifying a FUNCTION-ID paragraph. A user-defined function must return a value by specifying the RETURNING phrase of the procedure division header and they are invoked using the function-identifier syntax. See the ISO/IEC 1989:2014 Programming Language COBOL standard for details.
As of May 2022, the IBM Enterprise COBOL for [[z/OS]] 6.4 ([[IBM COBOL]]) compiler contains support for user-defined functions.
==Databases==
In
User-defined functions in SQL are declared using the <code>CREATE FUNCTION</code> statement. For example, a user-defined function that converts Celsius to Fahrenheit (a temperature scale used in USA) might be declared like this:
<
CREATE FUNCTION dbo.CtoF(Celsius FLOAT)
RETURNS FLOAT
RETURN (Celsius * 1.8) + 32
</syntaxhighlight>
Once created, a user-defined function may be used in [[expression (programming)|expressions]] in SQL statements. For example, it can be invoked where most other intrinsic functions are allowed.
<
SELECT Name, CtoF(BoilingPoint)
FROM Elements
</syntaxhighlight>
would retrieve the name and the boiling point from each row. It invokes the <code>CtoF</code> user-defined function as declared above in order to convert the value in the column to a value in Fahrenheit.
Each user-defined function carries certain properties or characteristics. The SQL standard defines the following properties:
Line 28 ⟶ 34:
*Specific name - a name for the function that is unique within the database. Note that the function name does not have to be unique, considering [[overloaded function]]s. Some SQL implementations require that function names are unique within a database, and overloaded functions are not allowed.
* Determinism - specifies whether the function is deterministic or not. The determinism characteristic has an influence on the [[query optimizer]] when compiling a SQL statement.
*SQL-data access - tells the database management system whether the function contains no SQL statements (NO SQL), contains SQL statements but does not access any tables or
User-defined functions should not be confused with [[stored procedure]]s. Stored procedures allow the user to group a set of SQL commands. A procedure can accept parameters and execute its SQL statements depending on those parameters. A procedure is not an expression and, thus, cannot be used like user-defined functions.
Line 35 ⟶ 41:
===SQL Server 2000===
There are three types of UDF in [[Microsoft SQL Server]] 2000: [[scalar function]]s, inline table-valued functions, and multistatement table-valued functions.
Scalar functions return a single data value (not a table) with RETURNS clause. Scalar functions can use all scalar data types, with exception of timestamp and user-defined data types.
Inline table-valued functions return the [[result set]] of a single SELECT statement.
Multistatement table-valued functions return a table, which was built with many TRANSACT-SQL statements.
Line 48 ⟶ 50:
Performance Notes:
<
CREATE FUNCTION MyFunction()
RETURNS @Tbl TABLE
Line 80 ⟶ 82:
RETURN
END
</syntaxhighlight>
User-defined functions are subroutines made of one or more Transact-SQL statements that can be used to encapsulate code for reuse.
Line 91 ⟶ 93:
Errors in UDF cause UDF to abort which, in turn, aborts the statement that invoked the UDF.
<
CREATE FUNCTION CubicVolume
-- Input dimensions in centimeters
Line 104 ⟶ 106:
RETURN(@CubeLength * @CubeWidth * @CubeHeight)
END
</syntaxhighlight>
===Apache Hive===
[[Apache Hive]] defines, in addition to the regular user
{{cite web
|first= |last=
|url=https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
|title=LanguageManual UDF - Apache Hive - Apache Software Foundation
|
|publisher=
|date=26 June 2015}}
Line 130 ⟶ 122:
|url=https://cwiki.apache.org/confluence/display/Hive/HivePlugins
|title=HivePlugins - Apache Hive - Apache Software Foundation
|
|publisher=
|date=26 June 2015}}
</ref>
===Apache Doris===
Apache Doris, an open-source real-time analytical database, allows external users to contribute their own UDFs written in C++ to it.<ref>{{cite web |title=Apache Doris UDF |url=https://doris.apache.org/docs/dev/ecosystem/udf/contribute-udf?_highlight=udf |access-date=8 April 2023 |archive-date=10 April 2023 |archive-url=https://web.archive.org/web/20230410141729/https://doris.apache.org/docs/dev/ecosystem/udf/contribute-udf/?_highlight=udf |url-status=dead }}</ref>
==References==
|