Skip to content

Commit 263b520

Browse files
committed
update file.md
1 parent 00709f5 commit 263b520

File tree

2 files changed

+41
-68
lines changed

2 files changed

+41
-68
lines changed

articles/azure-resource-manager/bicep/file.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ metadata <metadata-name> = ANY
2323
2424
targetScope = '<scope>'
2525
26+
func <user-defined-function-name> (<argument> <data-type>, <argument> <date-type>, ...) <function-data-type> => <expression>
27+
2628
@<decorator>(<argument>)
2729
param <parameter-name> <parameter-data-type> = <default-value>
2830
@@ -95,6 +97,18 @@ The allowed values are:
9597

9698
In a module, you can specify a scope that is different than the scope for the rest of the Bicep file. For more information, see [Configure module scope](modules.md#set-module-scope)
9799

100+
## Functions
101+
102+
Within your Bicep file, you can create your own functions in addition to using 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.
103+
104+
```bicep
105+
func buildUrl(https bool, hostname string, path string) string => '${https ? 'https' : 'http'}://${hostname}${empty(path) ? '' : '/${path}'}'
106+
107+
output azureUrl string = buildUrl(true, 'microsoft.com', 'azure')
108+
```
109+
110+
For more information, see [User-defined functions](./user-defined-functions.md).
111+
98112
## Parameters
99113

100114
Use parameters for values that need to vary for different deployments. You can define a default value for the parameter that is used if no value is provided during deployment.

articles/azure-resource-manager/bicep/user-defined-functions.md

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,106 +24,65 @@ To enable this preview, modify your project's [bicepconfig.json](./bicep-config.
2424

2525
## Define the function
2626

27-
You can use the `func` statement to define user-defined functions.
27+
Use the `func` statement to define user-defined functions.
2828

2929
```bicep
3030
func <user-defined-function-name> (<argument> <data-type>, <argument> <date-type>, ...) <function-data-type> => <expression>
3131
```
3232

33-
Your functions require a namespace value to avoid naming conflicts with template functions. The following example shows a function that returns a unique name:
33+
## Examples
34+
35+
The following examples show how to define and use user-defined functions:
3436

3537
```bicep
3638
func buildUrl(https bool, hostname string, path string) string => '${https ? 'https' : 'http'}://${hostname}${empty(path) ? '' : '/${path}'}'
3739
38-
func sayHello(name string) string => 'Hi ${name}!'
40+
func sayHelloString(name string) string => 'Hi ${name}!'
3941
40-
func objReturnType(name string) object => {
42+
func sayHelloObject(name string) object => {
4143
hello: 'Hi ${name}!'
4244
}
4345
44-
func arrayReturnType(name string) array => [
46+
func nameArray(name string) array => [
4547
name
4648
]
4749
48-
func asdf(name string) array => [
49-
'asdf'
50+
func addNameArray(name string) array => [
51+
'Mary'
52+
'Bob'
5053
name
5154
]
5255
53-
@minValue(0)
54-
type positiveInt = int
56+
output azureUrl string = buildUrl(true, 'microsoft.com', 'azure')
57+
output greetingArray array = map(['Evie', 'Casper'], name => sayHelloString(name))
58+
output greetingObject object = sayHelloObject('John')
59+
output nameArray array = nameArray('John')
60+
output addNameArray array = addNameArray('John')
5561
56-
func typedArg(input string[]) positiveInt => length(input)
5762
```
5863

59-
## Use the function
60-
61-
The following example shows a template that includes a user-defined function to get a unique name for a storage account. The template has a parameter named `storageNamePrefix` that is passed as a parameter to the function.
62-
63-
```json
64-
{
65-
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
66-
"contentVersion": "1.0.0.0",
67-
"parameters": {
68-
"storageNamePrefix": {
69-
"type": "string",
70-
"maxLength": 11
71-
}
72-
},
73-
"functions": [
74-
{
75-
"namespace": "contoso",
76-
"members": {
77-
"uniqueName": {
78-
"parameters": [
79-
{
80-
"name": "namePrefix",
81-
"type": "string"
82-
}
83-
],
84-
"output": {
85-
"type": "string",
86-
"value": "[concat(toLower(parameters('namePrefix')), uniqueString(resourceGroup().id))]"
87-
}
88-
}
89-
}
90-
}
91-
],
92-
"resources": [
93-
{
94-
"type": "Microsoft.Storage/storageAccounts",
95-
"apiVersion": "2022-09-01",
96-
"name": "[contoso.uniqueName(parameters('storageNamePrefix'))]",
97-
"location": "South Central US",
98-
"sku": {
99-
"name": "Standard_LRS"
100-
},
101-
"kind": "StorageV2",
102-
"properties": {
103-
"supportsHttpsTrafficOnly": true
104-
}
105-
}
106-
]
107-
}
108-
```
64+
The outputs from the preceding examples are:
10965

110-
During deployment, the `storageNamePrefix` parameter is passed to the function:
11166

112-
* The template defines a parameter named `storageNamePrefix`.
113-
* The function uses `namePrefix` because you can only use parameters defined in the function. For more information, see [Limitations](#limitations).
114-
* In the template's `resources` section, the `name` element uses the function and passes the `storageNamePrefix` value to the function's `namePrefix`.
67+
| Name | Type | Value |
68+
| ---- | ---- | ----- |
69+
| azureUrl | String | https://microsoft.com/azure |
70+
| greetingArray | Array | ["Hi Evie!","Hi Casper!"] |
71+
| greetingObject | Object | {"hello":"Hi John!"} |
72+
| nameArray | Array | ["John"] |
73+
| addNameArray | Array | ["Mary","Bob","John"] |
11574

11675
## Limitations
11776

11877
When defining a user function, there are some restrictions:
11978

12079
* The function can't access variables.
121-
* The function can only use parameters that are defined in the function. When you use the [parameters](template-functions-deployment.md#parameters) function within a user-defined function, you're restricted to the parameters for that function.
80+
* The function can only use parameters that are defined in the function.
12281
* The function can't call other user-defined functions.
123-
* The function can't use the [reference](template-functions-resource.md#reference) function or any of the [list](template-functions-resource.md#list) functions.
82+
* The function can't use the [reference](bicep-functions-resource.md#reference) function or any of the [list](bicep-functions-resource.md#list) functions.
12483
* Parameters for the function can't have default values.
12584

12685
## Next steps
12786

128-
* To learn about the available properties for user-defined functions, see [Understand the structure and syntax of ARM templates](./syntax.md).
129-
* For a list of the available template functions, see [ARM template functions](template-functions.md).
87+
* To learn about the Bicep file structure and syntax, see [Understand the structure and syntax of Bicep files](./file.md).
88+
* For a list of the available Bicep functions, see [Bicep functions](./bicep-functions.md).

0 commit comments

Comments
 (0)