<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSGraph/Builder/constant(name:value:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate9BNNSGraphO7BuilderV8constant4name5valueAE6TensorVy__xGSSSg_xtAA10BNNSScalarRzlF"
  },
  "title" : "constant(name:value:)"
}
-->

# constant(name:value:)

Registers and returns a tensor that contains a constant scalar value.

```
func constant<T>(name: String? = nil, value: T) -> BNNSGraph.Builder.Tensor<T> where T : BNNSScalar
```

## Parameters

`name`

The name of the tensor.

`value`

The scalar value that the function copies into the compiled context.

## Discussion

For example, the following code defines the tensor `x` that contains the single-precision
scalar value `10.0`:

```
       let context = try BNNSGraph.makeContext ({
           builder in

           let x = builder.constant(name: "x",
                                    value : Float(10))
           let y = builder.constant(name: "y",
                                    values : [1, 2, 3, 4, 5, 6, 7, 8] as [Float])

           let z = x + y

           return [z]
       })

       var results = [context.allocateTensor(argument: context.argumentNames()[0],
                                               fillKnownDynamicShapes: false)!]
       defer {
           results[0].deallocate()
       }
       try context.executeFunction(arguments: &results)
```

On return, the `results[0]` tensor contains the values `[11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0]`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)