Wednesday, April 25, 2012

Difference Between a Method and a Function

Simple Answer is to this question is function is just a part of code that executes code and can return something. A method is, in Object Oriented Programming, a function that is bound to a class. As in C# there are no stand-alone functions, every function in C# is a method just like in Java.

Let's start with properties. Properties are characteristics of an object. Examples of objects? JFrame, JButton in Java are objects. Objects can also be defined through class definitions and are instantiated when called upon and used in a program.

Methods are tasks that are tied directly to an object. They occur when an action takes place upon that object. They do not exist without the presence of that object.

Sub Routines (otherwise known as Independent Sub Functions) are functions that are completely INDEPENDENT of OBJECTS. That means that they can be called from anywhere in the program. They do not have a "return value," though they can return information to the calling function via parameter passing by reference.

Functions are code blocks thay create change on some variable or object. They are not necessarily tied to an object (though all methods are functions) and usually have a "return value" that is caught and used somehow in the calling function. Most often, this return value is stored in a variable, though the function call itself can be used in a subsequent function call (example: Dim sum as Integer = Add(getnumber() + 2) )