Functions

Description:

A function is a subprogram to group sequential statement and returns a value.
The important feature of functions is that they are used as expressions that return values of a specified type. This is the main difference from another type of subprograms: procedures, which are used as statements.
A function declaration consists of the name, parameter declaration and type of the value returned by the function.
The parameter list of a function takes the same form as that for a procedure, with two restrictions.

Syntax:


        function identifier [ (parameter_interface_list) ] return type_mark is
	    { subprogram_declarative_item }
        begin
	        { sequential_statement }
        end [ function ] [ identifier ];
        
    

Example 1:

A function that calculate the Area of a Circle.

        library IEEE;
        use IEEE.math_real.all;

        entity Circle is
        end Circle;

        architecture test of Circle is
            -- function
            function Area_Circle (constant radius: in real) return real is
            begin
                return radius * radius * MATH_PI;
            end Area_Circle;

            constant RADIUS : real := 2.1;
        begin
            process
                variable result : real;
            begin
                result := Area_Circle(RADIUS);
                report "Area of Radius " & real'image(RADIUS) & " is " & real'image(result);
                wait; -- Ensures the process does not run indefinitely
            end process;
        end architecture test;


    

Example:

This function in determines the number represented in binary by a bit-vector value.

        -- function declaration
        function bv_to_natural (bv : in bit_vector) return NATURAL is
	    variable result : NATURAL := 0;
        begin
	    for index in bv'range loop
		    result := result * 2 + BIT'pos(bv(index));
	    end loop;
	    return result;
        end function bv_to_natural;

        -- caller function
        data <= rom_data ( bv_to_natural(address) ) after Taccess;
        
    

Notes: