0
0
VhdlComparisonBeginner · 3 min read

Function vs Procedure in VHDL: Key Differences and Usage

In VHDL, a function returns a single value and cannot modify signals or variables outside its scope, while a procedure can have multiple outputs via parameters and can modify signals or variables. Functions are used for calculations, and procedures are used for actions or operations.
⚖️

Quick Comparison

This table summarizes the main differences between function and procedure in VHDL.

AspectFunctionProcedure
Return TypeReturns a single valueNo return value; uses parameters for output
Side EffectsCannot modify signals or variables outsideCan modify signals and variables via parameters
UsageUsed for calculations and expressionsUsed for operations and actions
ParametersOnly input parametersInput, output, and inout parameters allowed
Call ContextCan be called in expressionsCalled as a statement
Execution TimeMust execute quickly (no wait statements)Can include wait statements (in some contexts)
⚖️

Key Differences

Functions in VHDL are designed to return a single value and are pure in the sense that they cannot change signals or variables outside their local scope. They only accept input parameters and are typically used within expressions, such as assignments or conditions. Because of this, functions must execute quickly and cannot contain wait statements.

On the other hand, procedures do not return a value directly but can have multiple parameters including in, out, and inout. This allows procedures to modify variables or signals passed to them. Procedures are called as standalone statements and can perform actions or operations, including complex sequences. In some simulation contexts, procedures can include wait statements, allowing for timing control.

In summary, use functions when you need a computed value without side effects, and use procedures when you need to perform operations that may change multiple values or signals.

⚖️

Code Comparison

Here is an example of a function in VHDL that calculates the square of an integer.

vhdl
function square(x : integer) return integer is
begin
    return x * x;
end function square;
↔️

Procedure Equivalent

The equivalent procedure that calculates the square and returns it via an output parameter looks like this:

vhdl
procedure square_proc(x : in integer; result : out integer) is
begin
    result := x * x;
end procedure square_proc;
🎯

When to Use Which

Choose a function when you need to compute and return a single value without changing any external signals or variables, especially within expressions. Choose a procedure when you need to perform operations that may modify multiple outputs or signals, or when the operation involves multiple steps or side effects.

Key Takeaways

Functions return a single value and have no side effects outside their scope.
Procedures can modify multiple outputs via parameters and can have side effects.
Use functions for calculations and procedures for actions or operations.
Functions can be used inside expressions; procedures are called as statements.
Procedures allow more flexible parameter modes: in, out, and inout.