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.
This question is related to
c#
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...
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.
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<>
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.
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.
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?
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.
Source: Stackoverflow.com