Is a function an object?

Discussion in 'C Programming' started by E. Robert Tisdale, Aug 16, 2004.

  1. Is a function an object?
    E. Robert Tisdale, Aug 16, 2004
    #1
    1. Advertising

  2. E. Robert Tisdale

    JXStern Guest

    On Sun, 15 Aug 2004 18:02:17 -0700, "E. Robert Tisdale"
    <> wrote:
    >Is a function an object?


    By OOP convention and general connotations, I'd say no. We equate
    functions to verbs and objects to nouns, and demand an actor or locus
    (noun) as a thing that can perform the function (verb).

    That said, it doesn't take much to reify any function into a trivial
    object that does nothing but that function, and a variety of other
    hacks and intertranslations to other frameworks should be easy enough
    to propose.

    And why might you ask?

    J.
    JXStern, Aug 16, 2004
    #2
    1. Advertising

  3. E. Robert Tisdale

    Daniel T. Guest

    "E. Robert Tisdale" <> wrote:

    > Is a function an object?


    "A class can be thought of as a C-style function that can maintain state
    between invocations in a thread-safe manner and can also provide
    multiple services. If there were exactly one instance of a class, and
    all its member functions except for exactly one public member function
    were removed, the result would be a C-style function (the object's
    member data would correspond to static data that is local to the
    function.)" -- Marshall Cline et al. __C++_FAQs__ 2nd ed.
    Daniel T., Aug 16, 2004
    #3
  4. E. Robert Tisdale

    Alan Gauld Guest

    On Sun, 15 Aug 2004 18:02:17 -0700, "E. Robert Tisdale"
    <> wrote:

    > Is a function an object?


    It depends! In languages like Python functions are objects in
    their own right and can be queried for properties etc. Also in
    several languages code blocks can be defined and treated aas
    objects - Ruby, Lisp and Smalltalk are examples.

    OTOH most OO practitioners equate functions with the methods of
    an object rather than as first class objects in their own right.

    HTH,

    Alan G
    Author of the Learn to Program website
    http://www.freenetpages.co.uk/hp/alan.gauld
    Alan Gauld, Aug 16, 2004
    #4
  5. E. Robert Tisdale

    Thomas Gagne Guest

    I like Alan's explanation. He correctly prefaces it with, "It depends!"
    Functions existing as objects themselves is a powerful concept that escapes
    many programmers. It is worth investigating.
    Thomas Gagne, Aug 16, 2004
    #5
  6. "E. Robert Tisdale" <> writes:
    > Is a function an object?


    Warning: This question was cross-posted to comp.object and
    comp.lang.c. (I'm reading it in comp.lang.c.)

    In C terms, the answer is no, a function is not an object. We've just
    had a rather tiresome thread on the subject. The C standard has a
    reasonably precise definition of the term "object", and a function
    clearly does not qualify. (Note that the C standard's definition of
    "object" is not related to object-oriented programming; it's basically
    just a region of data storage, such as a variable.)

    Followups directed to comp.object.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 16, 2004
    #6
  7. E. Robert Tisdale

    Phlip Guest

    Phlip, Aug 16, 2004
    #7
  8. E. Robert Tisdale wrote:
    > Is a function an object?


    Note that Tisdale is trolling by posting this question to two newsgroups
    where the answer is different. I suppose he wants us to snip at each
    other. In C, a function is not an object. Trollsdale is trying to get
    the comp.object folk to tell the C folk that they are clueless. Please
    don't oblige him. I have set the followups to news:comp.lang.c only,
    since that's where I am. If folk on comp.object want to discuss this
    question, I suggest they change the newsgroup or follow-up headers to
    their newsgroup only.
    Martin Ambuhl, Aug 16, 2004
    #8
  9. "Thomas Gagne" <> wrote in message
    news:...
    > I like Alan's explanation. He correctly prefaces it with, "It depends!"
    > Functions existing as objects themselves is a powerful concept that

    escapes
    > many programmers. It is worth investigating.


    Whatever you call them, functions as those from C or many FPLs are not
    objects. "Functions existing as objects" may be interesting to _think_ of
    as functions (in the abstract), but are certainly not functions in the
    traditional sense.



    l8r, Mike N. Christoff
    Michael N. Christoff, Aug 16, 2004
    #9
  10. E. Robert Tisdale

    Fatted Guest

    Fatted, Aug 16, 2004
    #10
  11. E. Robert Tisdale wrote:

    > Is a function an object?


    Mr. Tisdale,

    Please define "object". Then define "function".

    If, in your definitions, a function can fit in the realm
    of, in your definition, of an object, then so mote it be.

    Secondly, why does it matter?

    I've used functions as objects (or more precisely,
    pointer to functions). I've used objects as functions
    (more precisely, pointers to functions that reside in
    a structure). Whether an object is a function or a
    function is an object has never precluded me from
    accomplishing my programming task.

    I'd rather be programming than debating the philophics
    of programming.


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 16, 2004
    #11
  12. Thomas Matthews wrote:

    > E. Robert Tisdale wrote:
    >
    >> Is a function an object?

    >
    >
    > Mr. Tisdale,
    >
    > Please define "object".


    Trollsdale posted to two newsgroups. In one of them, comp.lang.c,
    "object" is defined by the standard defining the language. He knows
    this. He also knows that a function is not an object in C. I have
    already asked people not to get sucked into this attempt to set people
    from comp.object, who have no more reason to know C than any other
    language, against the people from comp.lang.c, for whom there is nothing
    to discuss. *Please* don't play Trollsdale's game. If you must discuss
    this boring question, set the newsgroups or at least followups to point
    to your newsgroup only.

    Follow-ups set to comp.lang.c
    Martin Ambuhl, Aug 16, 2004
    #12
  13. E. Robert Tisdale

    Malcolm Guest

    "E. Robert Tisdale" <> wrote
    >
    > Is a function an object?
    >

    It comes down to semantics. In the C standard, "object" is defined as data
    occupying a contiguous region of storage. However if you said "what object
    does that pointer point to?" and someone replied "it's a function pointer to
    printf()" most people would accept the exchange as grammatical.

    In C++ you can have "function objects" which are a template-safe form of
    function pointer.

    In C, given approriate support, you can create data objects which specify
    functions. For instance you could write a parser that calculates a function
    of x and y, written in mathematical notation.
    Malcolm, Aug 17, 2004
    #13
  14. "Malcolm" <> writes:
    > "E. Robert Tisdale" <> wrote
    > >
    > > Is a function an object?
    > >

    > It comes down to semantics. In the C standard, "object" is defined as data
    > occupying a contiguous region of storage. However if you said "what object
    > does that pointer point to?" and someone replied "it's a function pointer to
    > printf()" most people would accept the exchange as grammatical.
    >
    > In C++ you can have "function objects" which are a template-safe form of
    > function pointer.
    >
    > In C, given approriate support, you can create data objects which specify
    > functions. For instance you could write a parser that calculates a function
    > of x and y, written in mathematical notation.


    If you're referring to a data structure that can specify the
    evaluation of some function, you can certainly do that, but it's not
    strictly speaking a "function" in the C sense.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 17, 2004
    #14
  15. Thomas Gagne <> wrote in
    news::

    > I like Alan's explanation. He correctly prefaces it with, "It
    > depends!" Functions existing as objects themselves is a powerful
    > concept that escapes many programmers. It is worth investigating.


    Yup. I have a fairly substantial numerical methods package (originally
    Smalltalk, now Java) that does function calculations, derivatives, interval
    calculations, statistics, blah blah blah, where functions are objects and
    practically everything else is done with the Visitor pattern.

    So roughly, if you have a function that is a "one-time" thing that belongs
    to another object, make it a method. But if you have a function that is
    going to be used for several things, then promote it to a first-class
    object.
    Rich MacDonald, Aug 18, 2004
    #15
    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.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,031
    Evan Carew
    Jan 21, 2004
  2. Petri Savolainen
    Replies:
    0
    Views:
    439
    Petri Savolainen
    Jul 1, 2003
  3. Gigi
    Replies:
    3
    Views:
    319
  4. jon wayne
    Replies:
    9
    Views:
    704
    Jim Langston
    Sep 22, 2005
  5. alex
    Replies:
    3
    Views:
    528
    Richard Cornford
    Dec 28, 2006
Loading...

Share This Page