Bird
Raised Fist0
VHDLprogramming~20 mins

Function and procedure in packages in VHDL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
VHDL Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a function call from a package
What is the output of the following VHDL code snippet when the function add_one is called with input 5?
VHDL
package math_pkg is
  function add_one(x : integer) return integer;
end package;

package body math_pkg is
  function add_one(x : integer) return integer is
  begin
    return x + 1;
  end function;
end package body;

architecture behavior of test is
  signal result : integer;
begin
  process
  begin
    result <= math_pkg.add_one(5);
    wait;
  end process;
end behavior;
Aresult = 6
Bresult = 5
Cresult = 0
DCompilation error due to missing package use clause
Attempts:
2 left
💡 Hint
Remember that the function adds 1 to the input value.
❓ Predict Output
intermediate
2:00remaining
Procedure call effect on signal in package
Given the following VHDL code, what is the value of signal out_val after the procedure set_value is called with input 10?
VHDL
package util_pkg is
  procedure set_value(signal s : out integer; val : integer);
end package;

package body util_pkg is
  procedure set_value(signal s : out integer; val : integer) is
  begin
    s <= val;
  end procedure;
end package body;

architecture behavior of test is
  signal out_val : integer := 0;
begin
  process
  begin
    util_pkg.set_value(out_val, 10);
    wait;
  end process;
end behavior;
ACompilation error due to procedure call syntax
Bout_val = 0
Cout_val = undefined
Dout_val = 10
Attempts:
2 left
💡 Hint
Procedures can assign values to signals passed as out parameters.
🧠 Conceptual
advanced
2:00remaining
Error caused by missing package body for function
What error will occur if a function declared in a VHDL package is used in design but its body is missing in the package body?
ALinker error: unresolved external symbol for the function
BSyntax error during compilation
CRuntime error: function returns default value
DNo error; function returns zero by default
Attempts:
2 left
💡 Hint
Think about what happens when the function implementation is missing.
❓ Predict Output
advanced
2:00remaining
Output of recursive function in package
What is the output of the following recursive function factorial defined in a package when called with input 4?
VHDL
package math_pkg is
  function factorial(n : integer) return integer;
end package;

package body math_pkg is
  function factorial(n : integer) return integer is
  begin
    if n = 0 then
      return 1;
    else
      return n * factorial(n - 1);
    end if;
  end function;
end package body;

architecture behavior of test is
  signal result : integer;
begin
  process
  begin
    result <= math_pkg.factorial(4);
    wait;
  end process;
end behavior;
ARuntime error due to infinite recursion
Bresult = 24
Cresult = 0
Dresult = 10
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.
🔧 Debug
expert
3:00remaining
Identify the error in procedure parameter passing
What error will the following VHDL code produce when compiling, considering the procedure parameter mode?
VHDL
package util_pkg is
  procedure update_val(signal s : in integer; val : integer);
end package;

package body util_pkg is
  procedure update_val(signal s : in integer; val : integer) is
  begin
    s <= val;
  end procedure;
end package body;

architecture behavior of test is
  signal data : integer := 0;
begin
  process
  begin
    util_pkg.update_val(data, 5);
    wait;
  end process;
end behavior;
ANo error; signal data updated to 5
BRuntime error: signal assignment ignored
CCompilation error: cannot assign to signal with mode 'in'
DSyntax error: missing 'out' keyword in procedure declaration
Attempts:
2 left
💡 Hint
Signals declared as 'in' mode cannot be assigned inside procedures.

Practice

(1/5)
1. What is the main difference between a function and a procedure in a VHDL package?
easy
A. A procedure returns a value, while a function does not.
B. A function returns a value, while a procedure does not.
C. Both return values but differ in syntax.
D. Neither returns a value; both perform actions only.

Solution

  1. Step 1: Understand function behavior

    A function in VHDL always returns a value and can be used in expressions.
  2. Step 2: Understand procedure behavior

    A procedure performs actions but does not return a value directly.
  3. Final Answer:

    A function returns a value, while a procedure does not. -> Option B
  4. Quick Check:

    Function returns value = B [OK]
Hint: Functions return values; procedures do not [OK]
Common Mistakes:
  • Thinking procedures return values
  • Confusing syntax differences with return behavior
  • Assuming both can be used in expressions
2. Which of the following is the correct way to declare a function inside a VHDL package?
easy
A. function add(a : integer; b : integer) return integer;
B. procedure add(a : integer; b : integer) return integer;
C. function add(a, b : integer);
D. procedure add(a : integer; b : integer);

Solution

  1. Step 1: Check function declaration syntax

    A function must specify parameters with types and a return type using return.
  2. Step 2: Validate options

    function add(a : integer; b : integer) return integer; correctly declares a function with parameters and return type. Options A and B are procedures, and C misses return type.
  3. Final Answer:

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

    Function declaration needs return type = D [OK]
Hint: Functions must declare return type explicitly [OK]
Common Mistakes:
  • Using 'procedure' keyword for functions
  • Omitting return type in function declaration
  • Adding return type to procedures
3. Given the following package body code, what is the output of calling result := math_pkg.multiply(3, 4);?
package body math_pkg is
  function multiply(x : integer; y : integer) return integer is
  begin
    return x * y;
  end multiply;
end math_pkg;
medium
A. 12
B. 7
C. Error: function not found
D. 0

Solution

  1. Step 1: Understand function multiply

    The function returns the product of x and y, so multiply(3,4) returns 3*4 = 12.
  2. Step 2: Confirm no errors

    The function is correctly declared and defined in the package body, so no errors occur.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply returns product of inputs [OK]
Common Mistakes:
  • Confusing addition with multiplication
  • Assuming function is undefined
  • Expecting procedure behavior
4. Identify the error in this package declaration:
package util_pkg is
  procedure display_message(msg : in string);
  function get_length(msg : string) return integer;
end util_pkg;
medium
A. Package missing body keyword.
B. Procedure missing return type.
C. Function parameter missing mode (in/out).
D. Procedure parameter mode is incorrect.

Solution

  1. Step 1: Check procedure parameter modes

    Procedure parameters must specify mode (in, out, inout). The procedure parameter msg correctly uses in.
  2. Step 2: Check function parameter modes

    Function parameters must also specify mode explicitly. The function parameter msg : string lacks mode, which is required.
  3. Final Answer:

    Function parameter missing mode (in/out). -> Option C
  4. Quick Check:

    Function parameters need mode = A [OK]
Hint: Always specify parameter mode in functions and procedures [OK]
Common Mistakes:
  • Omitting parameter mode in functions
  • Confusing package and package body syntax
  • Thinking procedures need return types
5. You want to create a VHDL package that includes a function to calculate the factorial of a number and a procedure to print the result. Which of the following is the best approach?
hard
A. Define factorial as a procedure modifying a global variable; print as a function returning string.
B. Define both factorial and print as functions returning integer.
C. Define factorial as a procedure returning integer; print as a procedure with no parameters.
D. Define the factorial as a function returning integer; define the print as a procedure taking integer input.

Solution

  1. Step 1: Choose correct return types

    Factorial calculation returns a value, so it should be a function returning integer.
  2. Step 2: Choose correct procedure for printing

    Printing is an action without return value, so it should be a procedure taking the integer to print.
  3. Final Answer:

    Define the factorial as a function returning integer; define the print as a procedure taking integer input. -> Option D
  4. Quick Check:

    Function returns value; procedure performs action = C [OK]
Hint: Use function for calculations, procedure for actions [OK]
Common Mistakes:
  • Using procedures to return values
  • Defining print as function with return value
  • Not passing parameters to procedures
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