Methods vs functions

  • Thread starter Thread starter Stefan
  • Start date Start date
S

Stefan

Could someone please explain if there is a difference between when one
should use the word method or function. Do they have the same meaning?
 
I generally use the term method, because thats how the framework refers to
them. The term method has no distinction between a Sub and a Function, and
can therefore mean a Sub or a Function. If you want to be explicit, then use
the terms 'Sub' or 'Function'. Also, in C-based languages, and indeed some
other languages, there are no subs or functions, and to create a 'sub' you
simply set the return type as void:

C++:

void MySub( )
{
return;
}

VB.NET:

Public Sub MySub()
Return
End Sub

C++:
int MyFunc( )
{
return 5;
}

VB.NET:

Public Function MyFunc() As Integer
Return 5
End Function

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit
 
Stefan,
Like Tom I use method for either Sub or Function.

I will also use method to mean Property procedures as well.

The way I view it is: Sub, Function & Property are types of methods. So I
use method when I want to be generic, I use Function when I need to be
specific.

Hope this helps
Jay
 
Hello,

Jay B. Harlow said:
Like Tom I use method for either Sub or Function.

That's how I use the term "method" too.
I will also use method to mean Property procedures as well.

I won't.
The way I view it is: Sub, Function & Property are types of methods. So I
use method when I want to be generic, I use Function when I need to be
specific.

I use the terms a bit different (according to
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconUnderstandingobjects.asp):

"methods" are actions, an object can perform.
"properties" are attributes.

Regards,
Herfried K. Wagner
 
Also, when compiled, IL creates two methods for the property, the get_...
and the set_... (unless its readonly or writeonly)

Just like operator overloading, IL creates overloaded versions op_...
methods to specify the different overloaded versions of the operators, which
technically means you can implement operator overloading in VB.NET, but
cannot *use* it in operator form.

Public Function op_Equality(ByVal o As Object, ByVal p As Object) As Boolean
Return o.ToString = p.ToString
End Function

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit
 
Hi,
I think this can be an endless discussion but important is in my opinion
again what language are you using (speaking).
My child name is Kok and from my sister Con.
My mother is not speaking any language.
You should have seen the faces from the french people when my mother was
calling us on the camping where we where when I was young.

I think that when I am thinking in VB I use the word function when I am
talking about functions like Mid, Len and even String.
I then am talking about methods when I use things like String.substring.

Cor
 
Hi Jay,

Jay B. Harlow said:
Although I agree with you 100%.

You did notice I was specific to say Property _Procedures_
(by Procedure I mean the Get & Set assessors) and not just
Property. As the Get & Set assessors have statement blocks aka action.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec7_6.asp

The problem is Properties are in that grey area where they're both.
Depending on what aspect of a property you are referring to.

The property itself is an attribute, while the get & set assessors are
'actions', which can be Overridden.

For example, You can apply the ConditionalAttribute to any method. This
includes the Get or Set assessor of a Property.

Property Name() As String
<Conditional("DEBUG")> _
Get
return m_name
End Get
<Conditional("DEBUG")> _
Set(ByVal value As String
m_name = value
End Set
End Property

Of course the above Get will not have the desired effect as its returning a
value, and needs to be called in all cases...

Which is where generically I will include Property Assessors as methods. At
the same time generically I include Properties as attributes.

Notice that I am not a native English speaker, maybe I am misinterpreting
the meaning of method, procedure, and attribute. Why not use the terms like
that:

method: Sub, Function (if they are members of a class)
procedure: Sub, Function, Property (Get, Set)
attribute: Property

Regards,
Herfried K. Wagner
 
The term attribute does tend to confuse, as that's what the < and > are
called:

<StructLayout(Foo)> Public Structure Bar
Public Baz As Moo
End Structure

The <StructLayout(Foo)> is an attribute, that's what microsoft calls it.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Hello,

Tom Spink said:
The term attribute does tend to confuse, as that's what the < and > are
called:

<StructLayout(Foo)> Public Structure Bar
Public Baz As Moo
End Structure

The <StructLayout(Foo)> is an attribute, that's what microsoft calls it.

ACK. The term attribute is overloaded, it applies to both, attributes, and
properties.

Regards,
Herfried K. Wagner
 
Tom,
Maybe Enlish it is not our native language but I think you misread
something.
Its funny :)) I had to smile.
Cor
 
Herfried,
Notice that I am not a native English speaker, maybe I am misinterpreting
the meaning of method, procedure, and attribute. Why not use the terms like
that:

method: Sub, Function (if they are members of a class)
When are they not a member of a class? Module & Interface perhaps? I
consider them methods in any Type (Class, Module, Interface, other).
procedure: Sub, Function, Property (Get, Set)
attribute: Property

For me method & procedure are synonymous (have the same or very nearly the
same meaning). Overall I consider procedure a type of method.

For me: Procedure is a carry over from RPG, COBOL, VB1 - VB6 days, while
Method is a carry over from C/C++/Java OOP days.

Because I favor OOP over VB6, I will tend to use method over procedure. When
I use procedure it will generally be in a VB6 context.

As you said Methods are actions, Property Get is an action & Property Set is
an action.

I consider both properties & fields to be attributes (lower case a). While
an Attribute (upper case A) adds meta data to any member or type.

Hope this helps
Jay
 
My response was a joke, e.g. we cannot yet perform operator overloading, so
why should we be able to overload attributes, even though it makes no
sense.... <g>

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Hello,

Tom Spink said:
My response was a joke, e.g. we cannot yet perform operator overloading, so
why should we be able to overload attributes, even though it makes no
sense.... <g>

I am not sure if you understood me...

;-)

Regards,
Herfried K. Wagner
 
Hello Jay,

Jay B. Harlow said:
When are they not a member of a class? Module & Interface perhaps? I
consider them methods in any Type (Class, Module, Interface, other).

Ooops. I forgot to mention interface, ...
For me method & procedure are synonymous (have the same or very nearly the
same meaning). Overall I consider procedure a type of method.

For me, methods are a subset of procedures. Some programming languages
don't allow defining classes etc., they allow procedures only. IMO these
procedures are not _methods_, because there are no objects (they do not
model an action an _object_ can perform).
For me: Procedure is a carry over from RPG, COBOL, VB1 -
VB6 days, while Method is a carry over from C/C++/Java OOP
days.
ACK.

Because I favor OOP over VB6, I will tend to use method over
procedure. When I use procedure it will generally be in a
VB6 context.
;-)

As you said Methods are actions, Property Get is an action &
Property Set is an action.

The implementation may perform an action, but it's not considered to be an
action. A property is used to model an attribute (no, not an attribute like
'ComVisible') of an object. If "an action" is performed in the property
procedure, I would write an explicit Get... or Set... method to make it
clearer, that I don't try to model an attribute. Notice that this is my
personal opinion.
I consider both properties & fields to be attributes (lower case a).
ACK.

While an Attribute (upper case A) adds meta data to any member
or type.

ACK.

Regards,
Herfried K. Wagner
 
LOL sorry. Let's just EOT it :-)

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Hi Jay,

You can obtain information about the members of a class using
System.Reflection [as you know :-)].

The help topic for MemberInfo says:
The MemberInfo class is the abstract base class of the classes used to
obtain information for all members of a class (constructors, events, fields,
<methods>, and <properties>).

Reflection includes both functions and subs under the tem methods. It
makes a distinction between methods and properties.

As I see it, a property (in its simplest form) is just a different
accessor for a field. I don't see fields as methods, and therefore don't see
properties as methods.

There are those who consider that writing a property that "takes action"
is bad design as they would define "action" as a side-effect - something not
desired in a property. It depends, of course, on how you define "action." :-)

Both "procedure" and "method" used to mean a routine that didn't return a
value, ie a Sub, but that distinction has been trampled on, RIP.

Just some thoughts.

Regards,
Fergus
 
Hi Cor,

|| You should have seen the faces from the french people
|| when my mother was calling us on the camping

LOL

|| My mother is not speaking any language

!!

Regards,
Fergus
 
Hello,

Cor said:
I think this can be an endless discussion but important is in
my opinion again what language are you using (speaking).
My child name is Kok and from my sister Con.

Cor is pronounced as Kok?
My mother is not speaking any language.
?!?

You should have seen the faces from the french people when
my mother was calling us on the camping where we where
when I was young.

How can your mother call you without speaking any language?
I think that when I am thinking in VB I use the word function
when I am talking about functions like Mid, Len and even String.
I then am talking about methods when I use things like
String.substring.

That's how I use them too. Substring is an action, a string can perform.
Mid, Len etc. are members of a class, but I treat them as functions because
they are not actions an object can perform.

Regards,
Herfried K. Wagner
 
Back
Top