Functions or Methods?

If I write abstract data types in C, are functions written to perform actions on these data types and displayed in the interface (.h files) called functions, methods, or something else completely different? I can't seem to find a constructive answer. Is a C ++ method a specific term?

+4
source share
6 answers

Is a C ++ method a specific term?

“Method” is an object-oriented programming term and refers to a function that is part of the namespace of an object. This way you can create methods for objects in languages ​​such as C ++, Java, Objective-C, etc. On the other hand, you still have standalone 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 specification.

+6
source

I always used the "method" as a synonym for the "member function", which implies C ++, since C does not have such a thing.

However, on the other hand, coins can be argued that the role of foo_alter_state is:

 struct foo { // implementation stuff; }; void foo_alter_state(struct foo *self); 

was still a method running on foo s, albeit independent. This is mainly OOP in C, but done manually.

This perspective may be especially relevant if struct foo; It was announced only in the headings, but was not defined at all publicly.

+4
source

Methods are functions associated with a class.

Since classes do not exist in C, everything is technically a function.

+3
source

The method is commonly used to describe "functions" that are part of a class in OOP. Functions are only those functions that are not part of the class.

In the .h file I would call them "function declarations" and in .c I would call them "function implementations" ... I am not an expert, but only my .02.

+2
source

I don’t think this is a particularly useful difference, but in general you would call them functions, because the word method implies a whole bunch of OOP things that C does not provide. Like the following :

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

But I regularly heard how people who grew up in Java viewed C functions as methods, and I never got confused about what they had in mind. Not even a bit.

However, you can make the following (free) differences:

  • A procedure is a block of code that does something

  • Function is a procedure that links one or more inputs to an output

  • Method is a procedure related to some class or instance of this class

+1
source

Strictly speaking, a function is an operation that provides an output for a given input. While a method is a type related function. C has only functions, I don’t think you think everything is different.

But if you implement ADT, for example, by putting function pointers in a structure (in functions that take a context pointer to an instance of the structure), so that you mimic an object using methods, then I see no reason to call them "methods implemented as functions" .

+1
source

Source: https://habr.com/ru/post/1393846/


All Articles