Ininformation technology andcomputer science, a system is described asstateful if it is designed to remember preceding events or user interactions;[1] the remembered information is called thestate of the system.
The set of states a system can occupy is known as itsstate space. In adiscrete system, the state space iscountable and oftenfinite. The system's internal behaviour or interaction with its environment consists of separately occurring individual actions or events, such as accepting input or producing output, that may or may not cause the system to change its state. Examples of such systems aredigital logic circuits and components,automata andformal language,computer programs, andcomputers.
The output of a digital circuit ordeterministic computer program at any time is completely determined by its current inputs and its state.[2]
Digital logic circuits can be divided into two types:combinational logic, whose outputsignals are dependent only on its present input signals, andsequential logic, whose outputs are a function of both the current inputs and the past history of inputs.[3] In sequential logic, information from past inputs is stored in electronic memory elements, such asflip-flops. The stored contents of these memory elements, at a given point in time, are collectively referred to as the circuit'sstate and contain all the information about the past to which the circuit has access.[4]
Since eachbinary memory element, such as a flip-flop, has only two possible states,one orzero, and there is a finite number of memory elements, a digital circuit has only a certain finite number of possible states. IfN is the number of binary memory elements in the circuit, the maximum number of states a circuit can have is2N.
Similarly, a computer program stores data invariables, which represent storage locations in thecomputer's memory. The contents of these memory locations, at any given point in the program's execution, are called the program'sstate.[5][6][7]
A more specialized definition of state is used for computer programs that operate serially or sequentially onstreams of data, such asparsers,firewalls,communication protocols andencryption. Serial programs operate on the incoming data characters or packets sequentially, one at a time. In some of these programs, information about previous data characters or packets received is stored in variables and used to affect the processing of the current character or packet. This is called astateful protocol and the data carried over from the previous processing cycle is called thestate. In others, the program has no information about the previous data stream and starts fresh with each data input; this is called astateless protocol.
Imperative programming is aprogramming paradigm (way of designing aprogramming language) that describes computation in terms of the program state, and of the statements which change the program state. Changes of state are implicit, managed by the program runtime, so that a subroutine hasvisibility of the changes of state made by other parts of the program, known asside effects.
Object-oriented programming attempts to improve on theimperative andprocedural programming paradigms byencapsulating related state (and behavior) withinobjects. The state of an object is oftenhidden in an attempt to isolate it from other objects and reducecoupling.[8] State that is hidden in this way is often referred to as the "internal state" of the object.
Indeclarative programming languages, the program describes the desired results and doesn't specify changes to the state directly.
Infunctional programming, state is usually represented withtemporal logic as explicit variables that represent the program state at each step of a program execution: a state variable is passed as aninput parameter of a state-transforming function, which returns the updated state as part of its return value. Apure functional subroutine only has visibility of changes of state represented by the state variables in its scope.
The output of a sequential circuit or computer program at any time is completely determined by its current inputs and current state. Since eachbinary memory element has only two possible states, 0 or 1, the total number of different states a circuit can assume is finite, and fixed by the number of memory elements. If there areN binary memory elements, a digital circuit can have at most 2N distinct states. The concept of state is formalized in an abstract mathematicalmodel of computation called afinite-state machine, used to design both sequential digital circuits and computer programs.
An example of an everyday device that has a state is atelevision set. To change the channel of a TV, the user usually presses a channel up or channel down button on the remote control, which sends a coded message to the set. In order to calculate the new channel that the user desires, the digital tuner in the television must have stored in it the number of thecurrent channel it is on. It then adds one or subtracts one from this number to get the number for the new channel, and adjusts the TV to receive that channel. This new number is then stored as thecurrent channel. Similarly, the television also stores a number that controls the level ofvolume produced by the speaker. Pressing the volume up or volume down buttons increments or decrements this number, setting a new level of volume. Both thecurrent channel andcurrent volume numbers are part of the TV's state. They are stored innon-volatile memory, which preserves the information when the TV is turned off, so when it is turned on again, the TV will return to its previous station and volume level.
As another example, the state of apersonal computer is the contents of all the memory elements in it. When computers such as laptops go intohibernation mode to save energy by shutting down the processor, the state of the processor is stored on the computer'shard disk, so it can be restored when the computer comes out of hibernation, and the processor can pick up where it left off.
page 46