Challenge - 5 Problems
Pure vs Impure Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a pure function in VHDL
What is the output of the following VHDL pure function when called with input 5?
VHDL
function double_val(x : integer) return integer is begin return x * 2; end function; -- Call: double_val(5)
Attempts:
2 left
💡 Hint
Pure functions always return the same output for the same input without side effects.
✗ Incorrect
The function doubles the input value. For input 5, output is 10.
❓ Predict Output
intermediateImpure function behavior in VHDL
What will be the output of this impure function when called twice with the same input 3?
VHDL
impure function counter(x : integer) return integer is variable count : integer := 0; begin count := count + x; return count; end function; -- Calls: -- counter(3) -- counter(3)
Attempts:
2 left
💡 Hint
Impure functions can have side effects but variables inside functions reset each call.
✗ Incorrect
The variable 'count' is local and resets to 0 each call, so output is always 3.
🧠 Conceptual
advancedIdentifying pure vs impure functions in VHDL
Which of the following VHDL functions is pure?
Attempts:
2 left
💡 Hint
Pure functions have no side effects and always return the same output for the same input.
✗ Incorrect
Option B is pure: no side effects, no reports, no variables that change state.
❓ Predict Output
advancedError type from impure function used in a pure context
What error will occur if an impure function is used in a context that requires a pure function in VHDL?
VHDL
impure function f(x : integer) return integer is begin return x + 1; end function; signal a : integer; begin a <= f(5); -- used in a pure context
Attempts:
2 left
💡 Hint
VHDL enforces purity rules strictly in certain contexts like signal assignments.
✗ Incorrect
Impure functions cannot be used where pure functions are required, causing compilation errors.
🧠 Conceptual
expertWhy use pure functions in VHDL?
Which is the main advantage of using pure functions in VHDL designs?
Attempts:
2 left
💡 Hint
Think about how pure functions affect design clarity and hardware synthesis.
✗ Incorrect
Pure functions have no side effects, so they are easier to understand and synthesize into hardware.
