83

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.

Scubacode
  • 415
  • 1
  • 4
  • 16
Ammar Asjad
  • 2,920
  • 6
  • 29
  • 42

8 Answers8

80

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

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Vitali Kaspler
  • 1,340
  • 10
  • 16
58

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 15
    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. – Eliran Malka Jun 03 '15 at 10:04
  • @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 question](http://stackoverflow.com/a/155633/961113) – Habib Jun 03 '15 at 12:17
  • 7
    i don't care about the implementation (C#), i was just saying the answer contradicts itself. – Eliran Malka Jun 03 '15 at 15:26
  • @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. – Habib Jun 03 '15 at 15:28
  • 2
    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 logic – Eliran Malka Jun 03 '15 at 15:30
  • @EliranMalka, so do you see a "function" in C# ? – Habib Jun 03 '15 at 15:32
  • 1
    again, i'm not referring C#, just the abstract concept of functions vs methods. maybe i'm too meticulous here, but semantics matter. – Eliran Malka Jun 03 '15 at 15:33
  • 5
    @EliranMalka, well the question is tagged in C# :) but again it is about terminology and everybody's own definition/understanding. – Habib Jun 03 '15 at 15:34
  • 1
    See @Vitali Kaspler's answer for a clearing up of this semantic argument. – Kristopher Oct 07 '15 at 13:50
  • 3
    @Habib They're not the same thing in C#. Take a look, for example, at [anonymous functions](https://msdn.microsoft.com/en-us/library/bb882516.aspx) and [anonymous methods](https://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx). – igorcadelima Dec 16 '15 at 15:09
  • There are several kinds of functions in C# namely methods, constructors, properties, events, indexers and finalizers etc. – RBT Oct 14 '16 at 01:35
14

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.

jdawg
  • 199
  • 1
  • 3
  • 11
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<>

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
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.

Freeman
  • 5,691
  • 3
  • 29
  • 41
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.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 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. – aj.toulan Nov 18 '15 at 04:23
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?

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
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.

Community
  • 1
  • 1