# Funciones


Las funciones HCL son ideales cuando necesitas manipular valores en tu configuración de compilación de formas más complejas que la simple concatenación o interpolación.

## Biblioteca estándar

Bake incluye soporte integrado para las [funciones de la biblioteca estándar](/build/bake/stdlib/).

El siguiente ejemplo muestra la función `add`:

```hcl {title=docker-bake.hcl}
variable "TAG" {
  default = "latest"
}

group "default" {
  targets = ["webapp"]
}

target "webapp" {
  args = {
    buildno = "${add(123, 1)}"
  }
}
```

```console
$ docker buildx bake --print webapp
```

```json
{
  "group": {
    "default": {
      "targets": ["webapp"]
    }
  },
  "target": {
    "webapp": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "buildno": "124"
      }
    }
  }
}
```

## Funciones definidas por el usuario

Puedes crear [funciones definidas por el usuario](https://github.com/hashicorp/hcl/tree/main/ext/userfunc) que hagan exactamente lo que deseas, en caso de que las funciones integradas de la biblioteca estándar no cubran tus necesidades.

El siguiente ejemplo define una función `increment`.

```hcl {title=docker-bake.hcl}
function "increment" {
  params = [number]
  result = number + 1
}

group "default" {
  targets = ["webapp"]
}

target "webapp" {
  args = {
    buildno = "${increment(123)}"
  }
}
```

```console
$ docker buildx bake --print webapp
```

```json
{
  "group": {
    "default": {
      "targets": ["webapp"]
    }
  },
  "target": {
    "webapp": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "buildno": "124"
      }
    }
  }
}
```

## Variables en funciones

Puedes hacer referencia a [variables](/build/bake/variables) y funciones de la biblioteca estándar dentro de tus funciones.

El siguiente ejemplo utiliza una variable global (`REPO`) en una función personalizada.

```hcl {title=docker-bake.hcl}
# docker-bake.hcl
variable "REPO" {
  default = "user/repo"
}

function "tag" {
  params = [tag]
  result = ["${REPO}:${tag}"]
}

target "webapp" {
  tags = tag("v1")
}
```

Imprimir el archivo de Bake con la bandera `--print` muestra que la función `tag` utiliza el valor de `REPO` para establecer el prefijo de la etiqueta (tag).

```console
$ docker buildx bake --print webapp
```

```json
{
  "group": {
    "default": {
      "targets": ["webapp"]
    }
  },
  "target": {
    "webapp": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "tags": ["user/repo:v1"]
    }
  }
}
```

