2021-08-21 12:38:19 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/statements/create/function
2022-04-09 13:29:05 +00:00
sidebar_position: 38
sidebar_label: FUNCTION
2023-05-24 16:40:12 +00:00
title: "CREATE FUNCTION -user defined function (UDF)"
2021-08-21 12:38:19 +00:00
---
2023-05-24 16:40:12 +00:00
Creates a user defined function (UDF) from a lambda expression. The expression must consist of function parameters, constants, operators, or other function calls.
2021-08-21 12:38:19 +00:00
**Syntax**
```sql
2022-07-04 11:24:54 +00:00
CREATE FUNCTION name [ON CLUSTER cluster] AS (parameter0, ...) -> expression
2021-08-21 12:38:19 +00:00
```
2021-08-22 19:57:59 +00:00
A function can have an arbitrary number of parameters.
2021-09-01 18:36:35 +00:00
2021-08-23 19:29:18 +00:00
There are a few restrictions:
2021-08-21 12:38:19 +00:00
2023-04-19 15:55:29 +00:00
- The name of a function must be unique among user defined and system functions.
- Recursive functions are not allowed.
- All variables used by a function must be specified in its parameter list.
2021-08-21 12:38:19 +00:00
2021-08-22 19:57:59 +00:00
If any restriction is violated then an exception is raised.
2021-08-21 12:38:19 +00:00
**Example**
Query:
```sql
2021-08-22 19:57:59 +00:00
CREATE FUNCTION linear_equation AS (x, k, b) -> k*x + b;
SELECT number, linear_equation(number, 2, 1) FROM numbers(3);
2021-08-21 12:38:19 +00:00
```
Result:
``` text
2021-09-04 16:31:04 +00:00
┌─number─┬─plus(multiply(2, number), 1)─┐
│ 0 │ 1 │
│ 1 │ 3 │
│ 2 │ 5 │
└────────┴──────────────────────────────┘
2021-08-22 19:57:59 +00:00
```
A [conditional function ](../../../sql-reference/functions/conditional-functions.md ) is called in a user defined function in the following query:
```sql
2021-09-04 16:31:04 +00:00
CREATE FUNCTION parity_str AS (n) -> if(n % 2, 'odd', 'even');
SELECT number, parity_str(number) FROM numbers(3);
2021-08-21 12:38:19 +00:00
```
2021-08-22 19:57:59 +00:00
Result:
``` text
2021-09-04 16:31:04 +00:00
┌─number─┬─if(modulo(number, 2), 'odd', 'even')─┐
│ 0 │ even │
│ 1 │ odd │
│ 2 │ even │
└────────┴──────────────────────────────────────┘
2021-08-22 19:57:59 +00:00
```
2022-12-14 15:01:15 +00:00
## Related Content
2023-05-12 12:05:38 +00:00
### [Executable UDFs](/docs/en/sql-reference/functions/udf.md).
### [User-defined functions in ClickHouse Cloud](https://clickhouse.com/blog/user-defined-functions-clickhouse-udfs)