There are two types of subprograms, functions and procedures. Functions are like mathematical functions and take zero or more inputs and compute a single value. Functions can be used in expressions such as, x+f(x), where f is some function. A procedure can have many inputs or outputs without one explicit return value. In addition to the two types of subprograms, there are two types of functions, pure and impure. Pure functions are functions which return the same value every time it is used with the same inputs, and impure otherwise. If unspecified, functions are assumed pure. If a function refers to non-local variables or signals, or accesses files, then it should be declared impure.
The rules for subprogram declarations are: The interface file is used to declare a file descriptor input to the subprogram. Files are discussed in the next chapter. The interface variable declares an input or output variable to the subprogram. The subprogram specification gives the interface to the subprogram by indicating its inputs, outputs and return value. If the FILE, SIGNAL, CONSTANT, or VARIABLE keyword does not appear in an interface declaration, then the object is a variable by default. Signals of mode INOUT and of a resolved type are not supported (NOTE that the same effect can be achived by using the same signal as two parameters, one of mode IN and the other of mode OUT).
Sometimes it is necessary to refer to a subprogram that has not yet been declared. In order to do this, a subprogram prototype should appear before that reference. The prototype does not give the statement body of the subprogram, but provides the interface which is necessary to call the subprogram.
A subprogram body is the list of statements that are executed when the subprogram is called. The subprogram body is a sequential program and thus contains only sequential statements. A number of different objects can be declared in a subprogram. They are listed in the subprogram declaration item rule. If the simple name appears at the end of the declaration, then it must repeat the name of the subprogram.
An example subprogram that converts an integer to a bit_vector is It has two integer inputs and returns a bit_vector. The ret and a variable declarations within the subprogram are called local variables because they may only be used within that subprogram.
There may be more than one subprogram with the same name as long as they can be distinguished by their parameter list. This feature is called subprogram overloading. For example, a function that accepts an integer input and a function that accepts a bit input can have the same name. When the function is called, the proper one is called according to the type of the actual parameter. Functions can also be used to do what is called operator overloading. A function can be declared to be designated by an operator name, overloading that operator. The syntax for an operator name is:
For example, overloads the + operator so that it may be used with bits as in