#### 11.3.84 `functor/3`

[ISO]

#### Synopsis

`functor(+Term, -Name, -Arity)`

`functor(-Term, +Name, +Arity)`

Succeeds if the principal functor of term `Term` has name
`Name` and arity `Arity`.

#### Arguments

`Term`- term

`Name`- atom

`Arity`- arity

#### Description

There are two ways of using this predicate:

- If
`Term` is initially instantiated, then
- if
`Term` is a compound term,
`Name` and `Arity` are unified with the name and arity of
its principal functor.
- otherwise,
`Name` is
unified with `Term`, and `Arity` is unified with 0.

- If
`Term` is initially uninstantiated, `Name` and
`Arity` must both be instantiated, and
- if
`Arity` is an integer in the range 1..255, then `Name` must
be an atom, and `Term`
becomes instantiated to the most general term having the specified
`Name` and `Arity`; that is, a term with distinct variables for all
of its arguments.
- if
`Arity` is 0, then
`Name` must be atomic, and it is unified with `Term`.

#### Exceptions

`instantiation_error`

`Term` and either `Name` or `Arity` are uninstantiated.

`type_error`

`Name` is not atomic, or
`Arity` is not an integer, or
`Name` is not an atom when `Arity` > 0.

`domain_error`

`Arity` is an integer < 0.

`representation_error`

`Term` is uninstantiated and `Arity` > 255.

#### Examples

| ?- `functor(foo(a,b), N, A).`
N = foo,
A = 2
| ?- `functor(X, foo, 2).`
X = foo(_A,_B)

| ?- `functor(X, 2, 0).`
X = 2

#### See Also

`arg/3`

, `name/2`

, `=../2`

, ref-lte-act.

Send feedback on this subject.