DIfference between function and procedure

Discussion in 'VHDL' started by Don Otknow, Mar 3, 2011.

  1. Don Otknow

    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
     
    Don Otknow, Mar 3, 2011
    #1
    1. Advertising

  2. Don Otknow

    Don Otknow Guest

    On Mar 3, 12:37 pm, Brian Drummond <>
    wrote:
    > On Thu, 3 Mar 2011 11:23:44 -0800 (PST), Don Otknow <>
    > 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 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
    >
    > >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.
     
    Don Otknow, Mar 3, 2011
    #2
    1. Advertising

  3. Don Otknow

    westocl

    Joined:
    Mar 7, 2011
    Messages:
    4
    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.
     
    westocl, Mar 8, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page