DIfference between function and procedure

D

Don Otknow

Hello,

I've never really understood the difference between a function and a
procedure in VHDL. I've read that a function returns 1 value whereas a
procedure can return multiple values? Could someone elucidate this for
me?

Also, I am confused about the scope of signals. If I declare a signal
within an architecture, does the scope of this signal extend to
functions declared within processes in the architecture? If not, is
there a way to do this without passing the signals? I would like to
call functions in a way resembling scripts that allowed for greater
code readability.

Thanks,
Don
 
D

Don Otknow

Same as in just about any language except the C family.

Function follows the basic pattern of a mathematical function; it (should) have
no side-effects and it returns a value. It is an abstraction over an expression;
it shouldn't change any term in that expression.

Procedure returns NO values (though some of its parameters may have OUT or INOUT
modes, which can be used to communicate results to the caller. ) It is an
abstraction over a statement (or block, i.e. sequence of statements).


Yes.
But please don't assign to that signal within a function!
You can assign to that signal within a procedure.

Functions and procedures (collectively, subprograms) can see the scope inwhich
they are declared - plus their own local variables. So they can not only see
signals outside the process, but any variables declared in the process (before
their own declaration!)

Parameters passed to the function or procedure come in useful when you want
different calls of the procedure to operate on DIFFERENT signals.

If you declare a library of useful subprograms in a package, you will find the
visibility rules are different there, and you will normally pass parameters to
them. One useful pattern is to keep the real subprograms there, and declare
simple (e.g. parameterless) wrapper subprograms within your process, which
simply call the real ones with appropriate parameters


Good idea.
But also; to allow programming - and hardware generation - at a higher level
than VHDL is typically used for.

- Brian

Thanks a lot Brian! Your explanations are very helpful.
 
Joined
Mar 7, 2011
Messages
4
Reaction score
0
Hello,

I've never really understood the difference between a function and a
procedure in VHDL. I've read that a function returns 1 value whereas a
procedure can return multiple values? Could someone elucidate this for
me?

As Brian said, can be used like a traditional mathmatical function and can return a value. A procedure does not return a value and cannot be used in expressions however a procedure can be used to drive multiple signals, not just one, so in a way a procedure is more flexable.

I would like to
call functions in a way resembling scripts that allowed for greater
code readability.

hmmm.. i think i know what you may be "thinking" of doing, and you will soon run into a problem if you are trying to write synthesizable code. The issue is functions and procedures cannot contain a memory element.. and when trying to describe hardware, one would generally want to be able to descrive a memory element. for instance, it would be great if we could write a function to describe a edge detect like..

A <= rise_edge_detect(B). , the issue is that we cant (unless you play with impure functions and such). because the lack of being able to describe a memory element So people generally abandon what i think you are trying to do to clean up code with subprograms, and you see many more Entity's where one would like to write a procedure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top