Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi dudes

i have a doubt that is
"What is the difference between function and method ? "
"How to find it ? "

Thanks
Posted
Comments
Ranjan.D 26-Oct-13 14:07pm    
How about extension methods and I assume you are getting confused now :) and please be specific when you say something in C#

http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

To the OP: what follows here may seem "academic," but the key point is that C# does not make the formal distinction between methods and functions that other programming languages require different syntax to express.

In communication with other programmers it may be useful if you describe a method that returns a value of some Type as a function, and refer to methods that return Type 'void as a method.

Mehdi Gholam is absolutely right when he points out that in common usage, in most programming languages, the distinction between methods and functions is that a function always returns a value, and a method does not return a value.

But, in C#, there's no special syntax for distinguishing between methods and functions ! All procedures (methods or functions) must specify a return Type: specifying a return Type of 'void is a way of telling the compiler that a method does not return a value.

The C# Programming Language specification [^] describes a method as:

"8.7.3 Methods

A method is a member that implements a computation or action that can be performed by an object or class. Methods have a (possibly empty) list of formal parameters, a return value (unless the method’s return-type is void), and are either static or non-static. Static methods are accessed through the class. Non-static methods, which are also called instance methods, are accessed through instances of the class. A generic method (§25.6) has a list of one or more type parameters."

The C# spec does frequently use the term "function members," as an over-arching summary category for all of: "static methods, static property accessors, and user-defined operators. Instance function members. These are instance methods, instance constructors, instance property accessors, and indexer accessors." See section 14.4.

Nowhere in the C# specification will you find a discussion of "functions" compared to "methods." And, you won't find the term "anonymous functions," or "lambda" in the spec, either.

In C# methods can be defined in both Classes, and Structs; and, yes, Structs can have static methods, even though a Struct cannot be defined as static: I've never been able to find a reason to use that feature :)
 
Share this answer
 
Comments
An@nd Rajan10 26-Oct-13 13:58pm    
can u give example...for each ???
BillWoodruff 26-Oct-13 22:58pm    
Time for you to do your homework. In the responses to your question here: you have everything you need to create examples.
An@nd Rajan10 27-Oct-13 8:48am    
ok dont waste your time ...
The commonly used definitions, be they correct or not CAN be:

A method is a member of a class that performs some action. A method can either be a subroutine (or procedure), something which does not return a value, or a function can be thought of as a procedure which does return a value.
 
Share this answer
 
Comments
An@nd Rajan10 26-Oct-13 13:58pm    
ok...some function have no return value that is possible....
then how to differentiate ???
Ranjan.D 26-Oct-13 14:40pm    
forget about function/method. Let us call as it's methods in C#. In order to differentiate look for the return type. If it's void then the method does not return a value.
PIEBALDconsult 27-Oct-13 1:35am    
There is no need to differentiate; which is why C-like languages don't.
Semantically a function returns values and a method does not, although in the .net CLR there is no distinction and they are both called methods.
 
Share this answer
 
Comments
Thomas Daniels 26-Oct-13 11:17am    
They are both called methods, because a method belongs to a class, but a function does not, so C# has no functions. Please see my answer.
PIEBALDconsult 26-Oct-13 11:29am    
But that is incorrect.
Thomas Daniels 26-Oct-13 11:29am    
Incorrect? Why?
An@nd Rajan10 26-Oct-13 13:55pm    
can you give an example
Ranjan.D 26-Oct-13 14:12pm    
Sorry I can't agree with "method does not return a value" , take an example of extension methods , they can either return or do not return a value.
 
Share this answer
 
Comments
An@nd Rajan10 26-Oct-13 14:00pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900