Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions or methods?

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 functions, methods, or something yet completely different? I can't seem to find a constructive answer anywhere. Is method a C++ specific term?

like image 554
Phonon Avatar asked Dec 03 '22 05:12

Phonon


2 Answers

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.

like image 163
Jason Avatar answered Dec 05 '22 19:12

Jason


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

struct foo {
  // implementation stuff;
}; 

void foo_alter_state(struct foo *self);

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

like image 44
Flexo Avatar answered Dec 05 '22 17:12

Flexo