Difference between function and method

In PHP I took reference from Beginning PHP 5 and MySql 5 By W Jason Gilmore, Appress book.
A method is quite similar to a function, except that it is intended to define the behavior of a particular class. Like a function, a method can accept arguments as input and can return a value to the caller. Methods are also invoked like functions, except that the method is prefaced with the name of the object invoking the method, like this:

Methods are created in exactly the same fashion as functions, using identical syntax. The only
difference between methods and normal functions is that the method declaration is typically
prefaced with a scope descriptor. The generalized syntax follows:
scope function functionName()
/* Function body goes here */
For example, a public method titled calculateSalary() might look like this:
public function calculateSalary()
return $this->wage * $this->hours;
In this example, the method is directly invoking two class fields, wage and hours, using the $this keyword. It calculates a salary by multiplying the two field values together, and returns the result just like a function might. Note, however, that a method isn’t confined to working solely with class fields; it’s perfectly valid to pass in arguments in the same way you can with a function.
Methods are invoked in almost exactly the same fashion as functions. Continuing with the previous example, the calculateSalary() method might be invoked like so:
$employee = new staff(“Janie”);
PHP supports six method scopes: public, private, protected, abstract, final, and static. The first five scopes are introduced in this section. The sixth, is static.
$salary = $employee->calculateSalary();
Public methods can be accessed from anywhere, at any time. You declare a public method by
prefacing it with the keyword public, or by foregoing any prefacing whatsoever. The following
example demonstrates both declaration practices, in addition to demonstrating how public
methods can be called from outside the class:

class Visitors
public function greetVisitor()
echo "Hello
function sayGoodbye()
echo "Goodbye
$visitor = new Visitors();

Now let’s see what happens in ASP.NET, or to be precise in C# :
I got a good answer from EggHeadCaffe
The major difference between methods and functions is that methods called by the reference variables called objects where as the functions do not having any reference variables.methods are invoked using objects.wheres as in case of functions here is no quesion of objects.we call it directly.
And yet another good answer from StackOverflow
A function is a piece of code that is called by name. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).

All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by name that is associated with an object. In most respects it is identical to a function except for two key differences.

It is implicitly passed the object for which it was called
It is able to operate on data that is contained within the class (remembering that an object is an instance of a class – the class is the definition, the object is an instance of that data)
(this is a simplified explanation, ignoring issues of scope etc.)


2 thoughts on “Difference between function and method

  1. hello
    i’m so glad that i found this website. that comment was so nice. thanks again i bookmarked this blog.
    are you planning to post similar posts?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s