Is a function an object?

J

JXStern

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

Daniel T.

E. Robert Tisdale said:
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.
 
A

Alan Gauld

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
 
T

Thomas Gagne

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

Keith Thompson

E. Robert Tisdale said:
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.
 
M

Martin Ambuhl

E. Robert Tisdale said:
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 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.
 
M

Michael N. Christoff

Thomas Gagne said:
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
 
T

Thomas Matthews

E. Robert Tisdale said:
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
 
M

Martin Ambuhl

Thomas said:
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
 
M

Malcolm

E. Robert Tisdale said:
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.
 
K

Keith Thompson

Malcolm said:
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.
 
R

Rich MacDonald

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.
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top