Procedure is unable to find function since it is defined after the procedure, how to fix this?

In my VHDL entity, I have declared some procedures in the header of the architecture i.e before the keyword begin. Some procedures are calling functions. The functions are actually defined in a block after the block that define the procedures. When I compile the code, I get "Unknown identifier" error.

I could just move the function definitions above the procedure definitions i.e cut paste the block of code or just shift it up in the source file. However, there must be a better way around this.

How do I write the code so the procedure knows that the function it is trying to call is actually defined after the procedure definition?

Answers 2

  • If the functions are defined in blocks inside the body of the entity (between the keywords "begin" and "end") then they are not in scope in the declarative part of the entity (between the keywords "is" and "begin") where you are defining the procedures.

    You need to move the function declarations into scope somehow. The most obvious way would be to put them in the declarative part of the entity declaration before the procedure declarations.

    entity foo is 
    end entity;
    architecture bar of foo is 
    --declare functions here
    --declare procedures here
        --don't declare functions here because procedures can't see them. 
        end block X;
    end bar;

    I don't see any way to solve this other than to move the function declarations into scope at the point where the procedures are declared.

    You could alternatively try defining the functions in a package an then referencing the package with a statement like "use work.my_package.all".

  • You don't, sadly.

    There's no such thing as forward declaration of functions outside of packages. It's a bit strange because the declaration <> body split in packages makes it seem as if the same split should work in other places, but it doesn't.

    Your two solutions are to move the functions in front of the procedures or split them off in a separate package which you use. But the full function has to be defined before using it.

Related Questions