DIfference between function and procedure

D

Don Otknow

Guest
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
 
On Thu, 3 Mar 2011 11:23:44 -0800 (PST), Don Otknow <donald.otknow@gmail.com>
wrote:

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?
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).

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?
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 in which
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

I would like to
call functions in a way resembling scripts that allowed for greater
code readability.
Good idea.
But also; to allow programming - and hardware generation - at a higher level
than VHDL is typically used for.

- Brian
 
On Mar 3, 12:37 pm, Brian Drummond <brian_drumm...@btconnect.com>
wrote:
On Thu, 3 Mar 2011 11:23:44 -0800 (PST), Don Otknow <donald.otk...@gmail.com
wrote:

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?

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).

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?

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 in which
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

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

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.
 

Welcome to EDABoard.com

Sponsor

Back
Top