Difference between Method and Function? [duplicate]
Asked Answered
E

8

84

I am a beginner in c# and have a keen interest to learn c#, but I am confused. When I asked some one what the difference is between Function and method, he said to me that there is no difference, that they both have the same functionality.
Now I am quite confused and want to know from good developers what methods and functions are?

Are they both the same? If not, then how do I initialize each one??

Is this way to initialize a function correct?

public void UpdateLeaveStatus(EmployeeLeave objUpdateLeaveStatus)

Please provide proper help as I am new.

Emanative answered 4/9, 2012 at 7:49 Comment(5)
Function is the process-oriented name, method the OO name. Since C# (and VB.NET) are object oriented languages you should use method.Merman
Mean to say that both are same ??Emanative
VB-Side-note: don't mix up Functions in VB.NET with functions, they are also methods with return values as opposed to Subs which don't return anything (but are also methods).Merman
Well, the difference is in the spelling. Just like the difference between soccer and football(not American football.)Endamoeba
And with regard to second part of your question, what do you want help with, function or method?Endamoeba
T
59

Both are same, there is no difference its just a different term for the same thing in C#.

Method:

In object-oriented programming, a method is a subroutine (or procedure or function) associated with a class.

With respect to Object Oriented programming the term "Method" is used, not functions.

Triquetrous answered 4/9, 2012 at 7:50 Comment(11)
the quote implies that there is a difference, and that a method is a special case of a function. please change your answer to reflect this.Mitch
@EliranMalka, there is no difference between a method and a function in C#, do you know any function in .Net framework ? It is mostly a matter of opinion, but most are of the view that in OOP there is only a method, See another answer on the linked questionTriquetrous
i don't care about the implementation (C#), i was just saying the answer contradicts itself.Mitch
@EliranMalka, where do you see method is a special case of function in the quoted statement in answer? for me it means a method is a subroutine/procedure or a function, so it could be any of those three things.Triquetrous
correct - if a method is (a kind of) a function that is associated with a class, there is a difference, hence they're not the same. just plain logicMitch
@EliranMalka, so do you see a "function" in C# ?Triquetrous
again, i'm not referring C#, just the abstract concept of functions vs methods. maybe i'm too meticulous here, but semantics matter.Mitch
@EliranMalka, well the question is tagged in C# :) but again it is about terminology and everybody's own definition/understanding.Triquetrous
See @Vitali Kaspler's answer for a clearing up of this semantic argument.Overrun
@Triquetrous They're not the same thing in C#. Take a look, for example, at anonymous functions and anonymous methods.Sidman
There are several kinds of functions in C# namely methods, constructors, properties, events, indexers and finalizers etc.Peers
P
84

When a function is a part of a class, it's called a method.

C# is an OOP language and doesn't have functions that are declared outside of classes, that's why all functions in C# are actually methods.

Though, beside this formal difference, they are the same...

Peeved answered 4/9, 2012 at 7:57 Comment(0)
T
59

Both are same, there is no difference its just a different term for the same thing in C#.

Method:

In object-oriented programming, a method is a subroutine (or procedure or function) associated with a class.

With respect to Object Oriented programming the term "Method" is used, not functions.

Triquetrous answered 4/9, 2012 at 7:50 Comment(11)
the quote implies that there is a difference, and that a method is a special case of a function. please change your answer to reflect this.Mitch
@EliranMalka, there is no difference between a method and a function in C#, do you know any function in .Net framework ? It is mostly a matter of opinion, but most are of the view that in OOP there is only a method, See another answer on the linked questionTriquetrous
i don't care about the implementation (C#), i was just saying the answer contradicts itself.Mitch
@EliranMalka, where do you see method is a special case of function in the quoted statement in answer? for me it means a method is a subroutine/procedure or a function, so it could be any of those three things.Triquetrous
correct - if a method is (a kind of) a function that is associated with a class, there is a difference, hence they're not the same. just plain logicMitch
@EliranMalka, so do you see a "function" in C# ?Triquetrous
again, i'm not referring C#, just the abstract concept of functions vs methods. maybe i'm too meticulous here, but semantics matter.Mitch
@EliranMalka, well the question is tagged in C# :) but again it is about terminology and everybody's own definition/understanding.Triquetrous
See @Vitali Kaspler's answer for a clearing up of this semantic argument.Overrun
@Triquetrous They're not the same thing in C#. Take a look, for example, at anonymous functions and anonymous methods.Sidman
There are several kinds of functions in C# namely methods, constructors, properties, events, indexers and finalizers etc.Peers
T
15

In C#, they are interchangeable (although method is the proper term) because you cannot write a method without incorporating it into a class. If it were independent of a class, then it would be a function. Methods are functions that operate through a designated class.

Tso answered 26/12, 2012 at 7:9 Comment(0)
H
3

There is no functions in c#. There is methods (typical method:public void UpdateLeaveStatus(EmployeeLeave objUpdateLeaveStatus)) link to msdn and functors - variable of type Func<>

Heroics answered 4/9, 2012 at 7:51 Comment(2)
All methods are functions. Not all functions are methods.Nevsa
Did you read my link to msdn? There is no term "function" in c#.Heroics
L
3

well, in some programming languages they are called functions others call it methods, the fact is they are the same thing. It just represents an abstractized form of reffering to a mathematical function:

f -> f(N:N).

meaning its a function with values from natural numbers (just an example). So besides the name Its exactly the same thing, representing a block of code containing instructions in resolving your purpose.

Lateral answered 4/9, 2012 at 7:52 Comment(0)
L
3

Both are the same, both are a term which means to encapsulate some code into a unit of work which can be called from elsewhere.

Historically, there may have been a subtle difference with a "method" being something which does not return a value, and a "function" one which does. in C# that would translate as:

public void DoSomething() {} // method
public int DoSomethingAndReturnMeANumber(){} // function

But really, I re-iterate that there is really no difference in the 2 concepts.

Lasky answered 4/9, 2012 at 7:53 Comment(1)
I always thought of it as function are stateless, and methods are functions that use/change the state of the object the function is associated with.Berga
R
3

From Object-Oriented Programming Concept:

If you have a function that is accessing/muttating the fields of your class, it becomes method. Otherwise, it is a function.

It will not be a crime if you keep calling all the functions in Java/C++ classes as methods. The reason is that you are directly/indirectly accessing/mutating class properties. So why not all the functions in Java/C++ classes are methods?

Roue answered 4/11, 2012 at 19:52 Comment(0)
L
2

Programmers from structural programming language background know it as a function while in OOPS it's called a method.

But there's not any difference between the two.

In the old days, methods did not return values and functions did. Now they both are used interchangeably.

Lail answered 4/9, 2012 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.