85

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.

5
  • 13
    Function is the process-oriented name, method the OO name. Since C# (and VB.NET) are object oriented languages you should use method. Commented Sep 4, 2012 at 7:51
  • Mean to say that both are same ?? Commented Sep 4, 2012 at 7:52
  • 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). Commented Sep 4, 2012 at 8:01
  • 4
    Well, the difference is in the spelling. Just like the difference between soccer and football(not American football.) Commented Sep 4, 2012 at 8:17
  • And with regard to second part of your question, what do you want help with, function or method? Commented Sep 4, 2012 at 8:19

8 Answers 8

87

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

Sign up to request clarification or add additional context in comments.

Comments

60

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.

12 Comments

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.
i don't care about the implementation (C#), i was just saying the answer contradicts itself.
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
@EliranMalka, well the question is tagged in C# :) but again it is about terminology and everybody's own definition/understanding.
@Habib They're not the same thing in C#. Take a look, for example, at anonymous functions and anonymous methods.
|
16

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.

Comments

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

2 Comments

All methods are functions. Not all functions are methods.
Did you read my link to msdn? There is no term "function" in c#.
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.

Comments

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.

1 Comment

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.