Bird
Raised Fist0
VHDLprogramming~10 mins

Pure vs impure functions in VHDL - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Pure vs impure functions
Call function
↓
Check inputs
↓
Compute output
↓
No side effects?
Yes→Return output
↓
Modify state or IO
When a function is called, it uses inputs to compute outputs. Pure functions return outputs without changing anything else. Impure functions may change other parts of the system.
Execution Sample
VHDL
shared variable sum : integer := 0;

function pure_add(a, b : integer) return integer is
begin
  return a + b;
end function;

function impure_add(a : in integer) return integer is
begin
  sum := sum + a;
  return sum;
end function;
Shows a pure function adding two numbers and an impure function that changes a variable each time it runs.
Execution Table
StepFunctionInputInternal StateOutputSide Effects
1pure_adda=2, b=3None5None
2pure_adda=2, b=3None5None
3impure_adda=2sum=0 before call2sum updated to 2
4impure_adda=3sum=2 before call5sum updated to 5
5impure_adda=1sum=5 before call6sum updated to 6
💡 Execution stops after showing multiple calls to both functions demonstrating pure and impure behavior.
Variable Tracker
VariableStartAfter Call 1After Call 2After Call 3Final
sum (impure_add)02566
Key Moments - 2 Insights
Why does pure_add always return the same output for the same inputs?
Because pure_add does not change any internal state or variables (see execution_table rows 1 and 2), it always returns the sum of inputs without side effects.
Why does impure_add output change even with different inputs?
impure_add changes the internal variable 'sum' each time it runs (see variable_tracker and execution_table rows 3-5), so output depends on previous calls, making it impure.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of 'sum' before the impure_add function runs?
A2
B0
C5
D3
💡 Hint
Check the 'Internal State' column at step 4 in the execution_table.
At which step does the pure_add function return output without any side effects?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Side Effects' column for pure_add calls in the execution_table.
If the variable 'sum' in impure_add was reset to 0 before each call, what would be the output at step 5?
A6
B5
C1
D0
💡 Hint
Consider that resetting 'sum' means no accumulation; output equals input at each call.
Concept Snapshot
Pure functions:
- Always return same output for same inputs
- No side effects (no state changes)
Impure functions:
- May change internal state or external data
- Output can depend on previous calls
In VHDL, pure functions are deterministic and safe for synthesis.
Full Transcript
This lesson shows the difference between pure and impure functions in VHDL. Pure functions always return the same output for the same inputs and do not change any internal or external state. Impure functions can change variables or states, so their output may vary depending on previous calls. The execution table traces calls to a pure_add function and an impure_add function, showing how pure_add returns consistent results without side effects, while impure_add updates an internal variable 'sum' and returns different outputs. The variable tracker highlights how 'sum' changes after each impure_add call. Key moments clarify why pure functions are predictable and impure functions are not. The quiz tests understanding by asking about internal states and outputs at specific steps. This helps beginners see how function purity affects behavior in VHDL.

Practice

(1/5)
1. Which statement best describes a pure function in VHDL?
easy
A. It always returns the same output for the same inputs and does not modify any external signals.
B. It can modify signals outside its scope and may return different outputs for the same inputs.
C. It only reads external signals but does not modify them.
D. It is a function that uses variables inside a process.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It always returns the same output for the same inputs and does not modify any external signals. -> Option A
  4. Quick Check:

    Pure function = consistent output, no side effects [OK]
Hint: Pure means no side effects and consistent output [OK]
Common Mistakes:
  • Confusing pure with impure functions
  • Thinking pure functions can modify signals
  • Assuming pure functions can read external signals
2. Which of the following is the correct way to declare a pure function in VHDL?
easy
A. procedure add(a: in integer; b: in integer);
B. function add(a: integer; b: integer) return integer is pure;
C. function add(a: integer; b: integer) return integer is impure;
D. function add(a: integer; b: integer) return integer;

Solution

  1. Step 1: Recall VHDL function declaration syntax

    In VHDL, to declare a pure function explicitly, you add the keyword pure after the function signature.
  2. Step 2: Identify correct syntax

    function add(a: integer; b: integer) return integer is pure; correctly uses is 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, and function add(a: integer; b: integer) return integer; lacks purity specification.
  3. Final Answer:

    function add(a: integer; b: integer) return integer is pure; -> Option B
  4. Quick Check:

    Pure function declaration includes 'is pure;' [OK]
Hint: Pure functions use 'is pure;' in declaration [OK]
Common Mistakes:
  • Omitting 'pure' keyword for pure functions
  • Confusing procedures with functions
  • Using 'impure' when pure is needed
3. Consider this VHDL function:
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?
medium
A. It is impure because it uses a variable that changes each call.
B. It is impure because it modifies an external signal.
C. It is impure because it uses variables inside a function.
D. It is a pure function because it returns a value based on input only.

Solution

  1. Step 1: Analyze variable usage inside function

    The local variable count is initialized to 0 each time the function is called, incremented to 1, and x + count always returns x + 1 for the same x.
  2. 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.
  3. Final Answer:

    It is a pure function because it returns a value based on input only. -> Option D
  4. Quick Check:

    Local variables reinitialized each call = pure [OK]
Hint: Local variables reset each call, pure function [OK]
Common Mistakes:
  • Thinking local variables make function impure
  • Confusing external signals with local variables
  • Believing the function output changes across calls
4. This VHDL function is intended to be pure:
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?
medium
A. The function cannot have parameters.
B. The function should be declared impure because it modifies result.
C. The function uses an undeclared variable result.
D. The function lacks a return statement.

Solution

  1. Step 1: Check variable declarations inside function

    The function uses result without declaring it as a variable or signal inside the function.
  2. Step 2: Understand VHDL variable scope rules

    All variables must be declared before use. Since result is undeclared, this causes a compilation error.
  3. Final Answer:

    The function uses an undeclared variable result. -> Option C
  4. Quick Check:

    All variables must be declared before use [OK]
Hint: Declare all variables before using them [OK]
Common Mistakes:
  • Assuming implicit variable declaration
  • Confusing pure/impure with variable declaration errors
  • Ignoring missing return statements
5. You want to create a function in VHDL that returns the current value of a global signal clk_count and increments it each call. Which is true about this function?
hard
A. It must be declared impure because it reads and modifies external signals.
B. It can be pure if it only returns the value without incrementing.
C. It can be pure if it uses a local variable to track increments.
D. It must be a procedure, not a function.

Solution

  1. Step 1: Understand function interaction with external signals

    Reading and modifying a global signal clk_count means the function has side effects and depends on external state.
  2. Step 2: Determine purity based on side effects

    Because the function changes external data, it cannot be pure and must be declared impure.
  3. Final Answer:

    It must be declared impure because it reads and modifies external signals. -> Option A
  4. Quick Check:

    Modifying external signals means impure function [OK]
Hint: Modifying external signals requires impure functions [OK]
Common Mistakes:
  • Thinking local variables can replace external signal changes
  • Assuming functions can modify signals without impure keyword
  • Confusing functions with procedures for side effects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard