Skip to content

Commit 5d3a7c5

Browse files
committed
Allow imported variable access from within User Defined Functions
1 parent 145a045 commit 5d3a7c5

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Imports in Bicep
33
description: This article describes how to import shared functionality and namespaces in Bicep.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 12/06/2024
6+
ms.date: 05/16/2025
77
---
88

99
# Imports in Bicep
@@ -47,6 +47,8 @@ Only statements that were [exported](#export-variables-types-and-functions) in t
4747

4848
You can use functionality that was imported from another file without restrictions. For example, you can use imported variables anywhere that a variable declared in-file would normally be valid.
4949

50+
Starting with [Bicep CLI version 0.31.X](https://github.com/Azure/bicep/releases/tag/v0.31.34), variables imported from other Bicep files are accessible within your user-defined functions, just like variables defined locally. For more information, see [User-defined functions](./user-defined-functions.md#define-functions).
51+
5052
### Example
5153

5254
*exports.bicep*

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ title: User-defined functions in Bicep
33
description: Describes how to define and use user-defined functions in Bicep.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 04/28/2025
6+
ms.date: 05/16/2025
77
---
88

99
# User-defined functions in Bicep
1010

1111
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. Using user-defined functions automatically enables [language version 2.0](../templates/syntax.md#languageversion-20) code generation.
1212

13-
[Bicep CLI version 0.26.X or higher](./install.md) is required to use this feature.
13+
[Bicep CLI version 0.26.X or higher](https://github.com/Azure/bicep/releases/tag/v0.26.54) is required to use this feature.
1414

1515
## Limitations
1616

1717
There are some restrictions when defining a user function:
1818

19-
* The function can't access variables.
2019
* The function can only use parameters that are defined in the function.
2120
* The function can't use the [reference](bicep-functions-resource.md#reference) function or any of the [list](bicep-functions-resource.md#list) functions.
2221
* Parameters for the function can't have default values.
@@ -30,8 +29,6 @@ Use the `func` statement to define user-defined functions.
3029
func <user-defined-function-name> (<argument-name> <data-type>, <argument-name> <data-type>, ...) <function-data-type> => <expression>
3130
```
3231

33-
## Examples
34-
3532
The following examples show how to define and use user-defined functions:
3633

3734
```bicep
@@ -71,7 +68,7 @@ The outputs from the preceding examples are:
7168
| nameArray | Array | ["John"] |
7269
| addNameArray | Array | ["Mary","Bob","John"] |
7370

74-
With [Bicep CLI version 0.23.X or higher](./install.md), you have the flexibility to invoke another user-defined function within a user-defined function. In the preceding example, with the function definition of `sayHelloString`, you can redefine the `sayHelloObject` function as:
71+
You have the flexibility to invoke another user-defined function within a user-defined function. In the preceding example, with the function definition of `sayHelloString`, you can redefine the `sayHelloObject` function as:
7572

7673
```bicep
7774
func sayHelloObject(name string) object => {
@@ -102,6 +99,55 @@ The output from the preceding example is:
10299
| ---- | ---- | ----- |
103100
| elements | positiveInt | 3 |
104101

102+
As of [Bicep CLI version 0.30.X](https://github.com/Azure/bicep/releases/tag/v0.30.3), user-defined functions can access variables defined in the same Bicep file.
103+
104+
## Example: Accessing variables in a user-defined function
105+
106+
The following example demonstrates how a user-defined function can reference a variable:
107+
108+
```bicep
109+
var greetingPrefix = 'Hello'
110+
111+
func greet(name string) string => '${greetingPrefix}, ${name}!'
112+
113+
output message string = greet('Azure')
114+
```
115+
116+
The output from the preceding example is:
117+
118+
| Name | Type | Value |
119+
| ------- | ------ | --------------- |
120+
| message | String | Hello, Azure! |
121+
122+
Starting with [Bicep CLI version 0.31.X](https://github.com/Azure/bicep/releases/tag/v0.31.34), variables imported from other Bicep files are accessible within your user-defined functions, just like variables defined locally.
123+
124+
Suppose you have a Bicep file named `shared.bicep` that exports a variable:
125+
126+
```bicep
127+
// shared.bicep
128+
@export()
129+
var sharedPrefix = 'Contoso'
130+
```
131+
132+
You can import this variable into your main Bicep file and use it inside a user-defined function:
133+
134+
```bicep
135+
import shared from './shared.bicep'
136+
137+
func makeResourceName(suffix string) string => '${shared.sharedPrefix}-${suffix}'
138+
139+
output resourceName string = makeResourceName('storage')
140+
```
141+
142+
The output from the preceding example is:
143+
144+
| Name | Type | Value |
145+
| ------------ | ------ | --------------- |
146+
| resourceName | String | Contoso-storage |
147+
148+
> [!NOTE]
149+
> For more details on importing variables, see [Export variables, types, and functions](./bicep-import.md#export-variables-types-and-functions).
150+
105151
## Use decorators
106152

107153
Decorators are written in the format `@expression` and are placed above function declarations. The following table shows the available decorators for functions.

0 commit comments

Comments
 (0)