Pure vs impure functions in VHDL - Performance Comparison
We want to see how the time it takes to run pure and impure functions changes as input grows.
How does the function type affect the number of steps needed?
Analyze the time complexity of the following VHDL functions.
function pure_add(a, b : integer) return integer is
begin
return a + b;
end pure_add;
function impure_accumulate(arr : in integer_array) return integer is
variable sum : integer := 0;
begin
for i in arr'range loop
sum := sum + arr(i);
end loop;
return sum;
end impure_accumulate;
The first function adds two numbers directly (pure). The second sums all elements in an array (impure).
Look for repeated steps that take time.
- Primary operation: The loop in
impure_accumulatethat adds each array element. - How many times: Once for each element in the array (n times).
The pure function always does the same small work, no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 addition |
| 100 | 1 addition |
| 1000 | 1 addition |
The impure function does more work as the array grows, adding each element once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: Pure function work stays the same; impure function work grows linearly with input size.
Time Complexity: O(1) for pure function, O(n) for impure function
Pure functions take the same time always; impure functions take longer as input grows.
[X] Wrong: "Pure functions always run slower because they do more checks."
[OK] Correct: Pure functions often do simple, fixed work without loops, so they usually run faster or the same regardless of input size.
Understanding how function types affect time helps you explain code efficiency clearly and confidently.
"What if the pure function called the impure function inside it? How would that change the time complexity?"
Practice
pure function in VHDL?Solution
Step 1: Understand pure function behavior
A pure function in VHDL must always return the same result for the same inputs and cannot change any external signals or variables.Step 2: Compare options with definition
It always returns the same output for the same inputs and does not modify any external signals. matches this definition exactly, while others describe impure or unrelated behaviors.Final Answer:
It always returns the same output for the same inputs and does not modify any external signals. -> Option AQuick Check:
Pure function = consistent output, no side effects [OK]
- Confusing pure with impure functions
- Thinking pure functions can modify signals
- Assuming pure functions can read external signals
pure function in VHDL?Solution
Step 1: Recall VHDL function declaration syntax
In VHDL, to declare a pure function explicitly, you add the keywordpureafter the function signature.Step 2: Identify correct syntax
function add(a: integer; b: integer) return integer is pure;correctly usesis pure;to declare a pure function. The procedure declaration is not a function,function add(a: integer; b: integer) return integer is impure;declares an impure function, andfunction add(a: integer; b: integer) return integer;lacks purity specification.Final Answer:
function add(a: integer; b: integer) return integer is pure; -> Option BQuick Check:
Pure function declaration includes 'is pure;' [OK]
- Omitting 'pure' keyword for pure functions
- Confusing procedures with functions
- Using 'impure' when pure is needed
function get_value(x: integer) return integer is variable count: integer := 0; begin count := count + 1; return x + count; end function;
What is true about this function?
Solution
Step 1: Analyze variable usage inside function
The local variablecountis initialized to 0 each time the function is called, incremented to 1, andx + countalways returnsx + 1for the samex.Step 2: Determine purity based on output consistency
Same inputs always produce the same output with no external modifications, making it pure. The claim about a changing variable ignores reinitialization, using variables inside is allowed, and there are no external signals.Final Answer:
It is a pure function because it returns a value based on input only. -> Option DQuick Check:
Local variables reinitialized each call = pure [OK]
- Thinking local variables make function impure
- Confusing external signals with local variables
- Believing the function output changes across calls
function multiply(a: integer; b: integer) return integer is pure; begin result := a * b; return result; end function;
What is the error in this code?
Solution
Step 1: Check variable declarations inside function
The function usesresultwithout declaring it as a variable or signal inside the function.Step 2: Understand VHDL variable scope rules
All variables must be declared before use. Sinceresultis undeclared, this causes a compilation error.Final Answer:
The function uses an undeclared variable result. -> Option CQuick Check:
All variables must be declared before use [OK]
- Assuming implicit variable declaration
- Confusing pure/impure with variable declaration errors
- Ignoring missing return statements
clk_count and increments it each call. Which is true about this function?Solution
Step 1: Understand function interaction with external signals
Reading and modifying a global signalclk_countmeans the function has side effects and depends on external state.Step 2: Determine purity based on side effects
Because the function changes external data, it cannot be pure and must be declared impure.Final Answer:
It must be declared impure because it reads and modifies external signals. -> Option AQuick Check:
Modifying external signals means impure function [OK]
- Thinking local variables can replace external signal changes
- Assuming functions can modify signals without impure keyword
- Confusing functions with procedures for side effects
Ready to prove it?
Pass the test and this pattern moves unknown
