1. def

Associates a key with a value in the current dictionary.

1.1. Syntax

key value def → –

1.2. Stack Effects

Table 1. Before
Level Object

1

key (usually a name)

0

value (any object)

Table 2. After
Level Object

(empty)

Current dictionary modified

1.3. Description

def associates key with value in the current dictionary (the one on top of the dictionary stack).

  • If key already exists: replaces its value

  • If key doesn’t exist: creates new entry

If the current dictionary is in global VM and value is a composite object in local VM, [invalidaccess] error occurs.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Basic definition
/x 42 def       % Define x as 42
/pi 3.14159 def % Define pi
Redefining values
/count 1 def
/count count 1 add def  % count now 2
Defining procedures
/square { dup mul } def
5 square  % Returns 25
Defining in specific dictionary
/mydict 10 dict def
mydict begin
  /localVar 100 def  % Goes into mydict
end

1.6. Common Use Cases

1.6.1. Variable Assignment

/width 612 def
/height 792 def
/margin 72 def

1.6.2. Procedure Definitions

/average { add 2 div } def
/max { 2 copy gt { exch } if pop } def

1.6.3. Constant Definitions

/BLACK 0 def
/WHITE 1 def
/GRAY 0.5 def

1.7. Common Pitfalls

Defines in Current Dictionary - def always uses the current dictionary (top of dict stack), not a specific dictionary.
% Wrong way to define in specific dict
/mydict 5 dict def
/x 42 def  % Goes into userdict, not mydict!

% Right way
mydict begin
  /x 42 def  % Goes into mydict
end
Global/Local VM Mixing - Cannot store local VM objects into global VM dictionaries.
true setglobal  % Global VM mode
/globalDict 10 dict def
globalDict begin
  false setglobal
  /localArray [1 2 3] def  % Error: local in global!
  true setglobal
end
Dictionary Full (Level 1) - In Level 1, dictionary must have capacity. In Level 2, auto-expands.
Use Literal Names - Always use literal names (starting with /) for keys.

1.8. Error Conditions

Error Condition

[dictfull]

Dictionary full (Level 1 only)

[invalidaccess]

Global VM dict receives local VM value

[limitcheck]

Dictionary expansion exceeds limits (Level 2)

[stackunderflow]

Fewer than 2 operands on stack

[typecheck]

Invalid operand types

[VMerror]

Insufficient VM for new entry or expansion

1.9. Implementation Notes

  • Most efficient way to create dictionary entries

  • In Level 2, may trigger dictionary expansion

  • Key typically a name, but can be any object

  • Definition is immediate (no delayed binding)

Operator Behavior

def

Define in current dictionary

store

Define in first dictionary containing key, or current if not found

put

Put into specific dictionary (requires dict on stack)

1.11. See Also

  • load - Look up value from dictionary stack

  • store - Store with different semantics

  • put - Put into specific dictionary

  • begin - Make dictionary current

  • where - Find which dictionary contains key


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.