|
| 1 | +--- |
| 2 | +title: User-defined functions in Bicep |
| 3 | +description: Describes how to define and use user-defined functions in Bicep. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.custom: devx-track-bicep |
| 6 | +ms.date: 08/30/2023 |
| 7 | +--- |
| 8 | + |
| 9 | +# User-defined functions in Bicep (Preview) |
| 10 | + |
| 11 | +Within your Bicep file, you can create your own functions. These functions are available for use in your Bicep files. User-defined functions are separate from the [standard Bicep functions](./bicep-functions.md) that are automatically available within your Bicep files. Create your own functions when you have complicated expressions that are used repeatedly in your Bicep files. |
| 12 | + |
| 13 | +[Bicep version 0.20 or newer](./install.md) is required to use this feature. |
| 14 | + |
| 15 | +## Enable the preview feature |
| 16 | + |
| 17 | +To enable this preview, modify your project's [bicepconfig.json](./bicep-config.md) file to include the following JSON: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "experimentalFeaturesEnabled": { |
| 22 | + "userDefinedFunctions": true |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +## Define the function |
| 28 | + |
| 29 | +Use the `func` statement to define user-defined functions. |
| 30 | + |
| 31 | +```bicep |
| 32 | +func <user-defined-function-name> (<argument-name> <data-type>, <argument-name> <data-type>, ...) <function-data-type> => <expression> |
| 33 | +``` |
| 34 | + |
| 35 | +## Examples |
| 36 | + |
| 37 | +The following examples show how to define and use user-defined functions: |
| 38 | + |
| 39 | +```bicep |
| 40 | +func buildUrl(https bool, hostname string, path string) string => '${https ? 'https' : 'http'}://${hostname}${empty(path) ? '' : '/${path}'}' |
| 41 | +
|
| 42 | +func sayHelloString(name string) string => 'Hi ${name}!' |
| 43 | +
|
| 44 | +func sayHelloObject(name string) object => { |
| 45 | + hello: 'Hi ${name}!' |
| 46 | +} |
| 47 | +
|
| 48 | +func nameArray(name string) array => [ |
| 49 | + name |
| 50 | +] |
| 51 | +
|
| 52 | +func addNameArray(name string) array => [ |
| 53 | + 'Mary' |
| 54 | + 'Bob' |
| 55 | + name |
| 56 | +] |
| 57 | +
|
| 58 | +output azureUrl string = buildUrl(true, 'microsoft.com', 'azure') |
| 59 | +output greetingArray array = map(['Evie', 'Casper'], name => sayHelloString(name)) |
| 60 | +output greetingObject object = sayHelloObject('John') |
| 61 | +output nameArray array = nameArray('John') |
| 62 | +output addNameArray array = addNameArray('John') |
| 63 | +
|
| 64 | +``` |
| 65 | + |
| 66 | +The outputs from the preceding examples are: |
| 67 | + |
| 68 | + |
| 69 | +| Name | Type | Value | |
| 70 | +| ---- | ---- | ----- | |
| 71 | +| azureUrl | String | https://microsoft.com/azure | |
| 72 | +| greetingArray | Array | ["Hi Evie!","Hi Casper!"] | |
| 73 | +| greetingObject | Object | {"hello":"Hi John!"} | |
| 74 | +| nameArray | Array | ["John"] | |
| 75 | +| addNameArray | Array | ["Mary","Bob","John"] | |
| 76 | + |
| 77 | +## Limitations |
| 78 | + |
| 79 | +When defining a user function, there are some restrictions: |
| 80 | + |
| 81 | +* The function can't access variables. |
| 82 | +* The function can only use parameters that are defined in the function. |
| 83 | +* The function can't call other user-defined functions. |
| 84 | +* The function can't use the [reference](bicep-functions-resource.md#reference) function or any of the [list](bicep-functions-resource.md#list) functions. |
| 85 | +* Parameters for the function can't have default values. |
| 86 | + |
| 87 | +## Next steps |
| 88 | + |
| 89 | +* To learn about the Bicep file structure and syntax, see [Understand the structure and syntax of Bicep files](./file.md). |
| 90 | +* For a list of the available Bicep functions, see [Bicep functions](./bicep-functions.md). |
0 commit comments