¿Funciones o métodos?

If I'm writing abstract data types in C, are the functions written to perform actions on these data types and exposed in the interface (.h files) called funciones, métodos, or something yet completely different? I can't seem to find a constructive answer anywhere. Is Método a C++ specific term?

preguntado el 30 de enero de 12 a las 19:01

6 Respuestas

Is method a C++ specific term?

A "method" is an object-oriented programming term, and refers to a function that is part of the namespace of an object. So you can create methods for objects in languages like C++, Java, Objective-C, etc. In C on the otherhand, you still have stand-alone functions, not methods.

Keep in mind that the "official" C++ term for a class method is a "member function", and is described in section 9.3 of the spec.

Respondido el 31 de enero de 12 a las 00:01

I've always used "method" as a synonym for "member function", which implies C++ since C has no such thing.

On the other side of the coin however, one might argue that the role of foo_alter_state en:

struct foo {
  // implementation stuff;
}; 

void foo_alter_state(struct foo *self);

fue still a method operating on foos, albeit free-standing. It's basically OOP in C, but implemented by hand.

This perspective might be particularly true if struct foo; was only ever declared in headers, but not defined publicly anywhere.

Respondido el 31 de enero de 12 a las 00:01

Agreed, which is why I say that it's not very useful to get too serious about the distinction. - Wayne

Methods are functions associated with a class.

Since classes don't exist in C, everything is technically a function.

Respondido el 31 de enero de 12 a las 00:01

Method is typically used to describe "functions" that are part of a class in OOP. Functions are just that - functions, not part of a class.

In the .h file I'd call them "function declarations" and in the .c I'd call them "function implementations" ... I'm no expert though, just my .02.

Respondido el 31 de enero de 12 a las 00:01

Strictly speaking a function is an operation that gives an output for a given input. Whereas a method is a function bound to a type. C only has functions, I don't think you think any different.

But if you are implementing an ADT by, for example, putting function pointers in a struct (to functions that take a context pointer to the struct instance), so that you're mimicking an object with methods, then I see no reason to call them "methods implemented as functions".

Respondido el 31 de enero de 12 a las 00:01

I don't think this is a particularly useful distinction to make, but in general, you'd call them funciones porque la palabra Método implies a whole bunch of OOP stuff that C doesn't provide. Like el siguiente:

Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance.

But I've routinely heard folks who grew up on Java refer to C funciones as métodos and I've never once been confused about what they meant. Not even a little bit.

You might, however, want to make the following (loose) distinctions:

  • A procedimientos is a block of code that does something

  • A función is a procedure that associates one or more inputs with an output

  • A Método is a procedure that belongs to some class or an instance of that class

Respondido el 31 de enero de 12 a las 00:01

I'd agree with this in general, although I've seen quite a strong correlation between people who use method/function interchangeably and people who view C as "poor man's Java", so it can make an interesting discussion point. - Flexografía

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.