Dear brothers,
Hope you all are well :)
I just wonder how should we call the above void foo() correctly -Code:void foo() {}
Method? or Function?
Printable View
Dear brothers,
Hope you all are well :)
I just wonder how should we call the above void foo() correctly -Code:void foo() {}
Method? or Function?
Books writing about this is out of date, Mary-go-around story
I like face-to-face talks about this to see how small or how big I am. That's the way to learn not via net.
I am looking too for
1. documents about functions
2. differences of function and methods
3. When function is called when method is called
Functions which belong to class objects may be called methods. Non-class functions are not.
I would subscribe to Lindley's definition if it lacked the word "objects", which causes it to disagree with the term "static method". To its credit, however, that definition does underscore an important point to be made concerning functions: non-static member functions (i.e. methods) are incompatible with function pointers of the equivalent signature for the reason that they require an object in order to be invoked.
1) As already mentioned, the standard never uses the word "method"
in this context.
2) In TC++PL3, Stroustrup states:
"a virtual member function is sometimes called a method".
As Philip Nicoletti pointed out, Stroustrup's definition of method in C++ is virtual member function. That Wikipedia article is more general in nature.
Well, I don't think C++ actually defines the term "method". But it's still interesting to know what Stroustrup considers to be a method in C++.
Method is an OO term and it denotes what an object can do, that is its behaviour. Because the ability to change an object's behaviour by the use of inheritance is an important OO feature, Stroustrup limits what should be called a method to functions exhibiting so called runtime polymorphism, namely virtual functions in C++.
In Java on the other hand the term "function" simply isn't used. Every "function" is called a method regadless of properties or usage.
Thanks for all inputs ! :)
I have seen few C++ programmers call as a "Method". Also, they claim, they are really methods. But they couldn't give any convincing answer for why they call so.
From the replies I can understand C++ standard tries to refer as "function". I don't know what factor separates a method and a function.
I've tried to explain that. Method is an OO term. A method is a function that's related to an object's behaviour.
Java is considered so OO that every function is called a method (by definition, function isn't used at all).
C doesn't have any OO so no function should be called a method.
In C++, all functions defined in a class (in principle the part of C++ that isn't C) could be called methods (this would then be equivalent to what's considered a method in Java). Stroustrup has a stricter definition. He thinks only functions which are virtual should be called methods.
Yes, Plasmator already pointed out that the C++ standard does not use nor define the term "method". On the other hand, there are many C++ terms that are not defined or even mentioned in the C++ standard, so I think that it is fair to consider Stroustrup's definition as authoritative concerning C++.Quote:
Originally Posted by _uj
If we look at it from an object oriented perspective rather than a language specific perspective, then I would argue that in any program in which OOP is used, there are methods, even if the program's programming language does not have native support for OOP, and "method" is not in that language's terminology.Quote:
Originally Posted by _uj
In other words, we could call member functions methods. However, that does not really answer thomus07's question: "what factor separates a method and a function"?Quote:
Originally Posted by _uj
Besides syntax, the difference between a member function and a non-member function is that the member function can access the object's (or class') internals. If that is used as the criterion, then what about friend functions?
The decisive factor is how the function is used. If it defines object behaviour it's a method (according to the OO terminology).
In my view in C++ it's questionable whether static functions and private function should be called methods. In Java they are but that's probably only because every function is called a method there.
Add this choise to the poll,
- Method if it defines object behaviour, otherwise function.
What does "defines object behaviour" really mean?Quote:
Originally Posted by _uj
Yes, the term would be language specific in that context.Quote:
Originally Posted by _uj
Thanks _uj. All your answers are very informative.
The poll is not a judgment of What void foo() - is a Method? or Function?
It is what we are discussing.
The poll is about - How do you call / refer to void foo() in all our C++ programming career. In the sense some people call it as method and some as function.
Because modern languages like C#, refers as "Method". Even in this perspective static methods won't define object's behavior. Still C# calls everything as Method.
So I think it is more towards - A LANGUAGE PERSPECTIVE than the actual concept.
"When in Rome, do as the Romans do."Quote:
Originally Posted by thomus07
Therefore, I would suggest that you call foo a "function", not a "method", in C++. However, if you are discussing such constructs with a group of programmers more familiar or comfortable with the term "method", by all means use "method".
If we consider that "function" in most programming languages indicates a block of callable code [although some do restrict it to those cases that return a value].....Then we consider "method" to mean "behaviour".
We begin to see where Stroustrup's selectionof virtual functions to be called methods makes alot of sense.
static members and non-virtual instance members have a definite 1:1 mapping to a specific block of code. This code can be called a "function" if you accept my first paragraph.
virtual members on the other hand, truely do specify a behaviour, which can be abstract (pure), have a single implementation (no-overrides) or multiple implementations (overrides in different derived classses).
Therefore you can NOT say "a virtual member is a function" (note the emphasis on the singular).
Thanks cpuwizard.
Your analysis are very excellent.
You could say some valid points to support non-virtual members as functions.
I think most languages refer either of the way. And it is advised to adopt consistently for that language.
This may be because this couldn't create any side effect.
A deep analysis could say that, in some contexts this generalization is not suitable.
To refer correct name in appropriate context will make correct sense undoubtedly. Also correct reference gives a better understanding of the language.
If we don't want if we can forget this may be :).
No matter how many times I've heard these arguments, I've come to the conclusion that a function vs. a method is really is only a symantac difference. Heck, if you ported it to Pascal it would be a Procedure. My advice, roll with whatever term the language calls it (or whatever your instructor calls it, for the duration you are in his/her class).
just for kicks:
Code:{Pascal version of foo, not case sensitive}
Procedure foo;
begin
end;
{variation with parameters}
Procedure foo2(someValue: Integer);
begin
someValue:=someValue+1;
end;
{variation as a Function}
Function foo3(someValue: Integer): Integer;
begin
foo3:=someValue*3;
end;