Expert Group of programming
Loading...

Difference between Method and function

Posted by : Heycode Tech | Posted in : Interview Type, jQuery, programming | Oct 9,2015 | 0 comments

It is an very important topic and generally ask in interview. it’s very confusion for mostly programmer for displaying the difference.

Now here I will explain about the difference in many languages like C, C++ , C# , JAVA and JAVA SCRIPT.

Differences are :

1: When any function call directly i.e call by function name only , then these functions are called function.

while when any function call by the help of object i.e objectName.functionName then this function is called Method.

//function main in C language 
void main(){
int x1 , x2, x3;
x1= 5;
x2=6;
x3 = x2 - x1;
printf("Sum is %d",x3);
}
main function will call without any object so it is called function.

 

//in java language 
public class heycodetech{
 public substraction(int a,int b){
   int c;
     c = a - b;
    system.out.println("The value of c = "+c);
   }

}
This function can't be call automatically so we need to create an object for calling it.
//create instance of object
    heycodetech Obj = new heycodetech();
    //call instance method using object
    Obj .substraction();

2 : Function can be used in both (Object oriented and Non object oriented) Like C, C++, Javascript .
While Method can be found in only Object oriented language like java, c#.

3: Functions are called independently .
while Method are called using instance or object.

4: It doesn’t have access controlling i.e Function declared and define it’s body anywhere.
while Method have access controlling i.e Declared and define in a class.

5: Function don’t have any reference variable.
while Method is called by reference variable.

6:In javascript, let’s see an example .

function heycodetech(par1,par2){
 console.log("Both parameter value : 1 =  " +par1+ " , 2 = "+par2);
}

//It is called a function because I can call it directly.
   heycodetech("Tony","jai");

Now another way of creating function .

var obj = {
           name:"hey",
           2,
          heycodetech:function(par1,par2){
                 console.log("Both parameter value : 1 =  " +par1+ " , 2 = "+par2);
            }
   }
//for calling this method, I need to use object : obj
   obj.heycodetech("Tony","jai");
So it is an Method.

Hope by using of this information, you can understand about difference between : Function and Method : .
Please share the own idea about it also .

Tags:

Author information - Heycode Tech

Every world can ask question and my website registered user give the suggestion.

See all posts by Author

Connect to author

About us

Heycodetech is a platform where you can ask any questions related to the programming like PHP,jQuery, MYSQL,HTML , CSS and moreHere is the Expert group of programming.