You must Sign In to post a response.
  • Category: Programming

    What is the difference between function and method in programming?


    Want to know about some basic concepts in computer programming? Looking out for knowing the difference between function and method? Check out this page for answers to your questions from experts.

    I would like to know the difference between function and method in computer programming.
    Almost all the computer programming and scripting languages have this feature to use in code. I know both of them will perform similar action of taking an input, executing some statements and providing the output but I am unable to figure out the difference and what is the right place to use function and method.
    It would be good if anyone in ISC who have knowledge on programming can explain to me with a proper example.
  • #147908
    Method and function pretty much work the same way. However method was the name given to the subroutines before the OOP. And function and subroutine name was used by the procedural languages in past. After the introduction to OOP. The methods name is in use to make sure that the programming language is addressing the routine in a specific way. As function and functionalities in OOP tend to clash in the tech jargon. So methods are used over the function naming. And "subroutine" and "function" is not much used in any modern programming language. It's more to do with semantics than the any specific technical reasons.

  • #147913
    Functions are defined outside of the class whereas methods are defined within the class.
    Functions are defined in structured language whereas methods are defined in object-oriented languages.
    Functions can be called independently whereas methods can be called using instance or object.
    Functions are self-describing unit of code whereas methods are used to manipulated variables of a class

    drrao
    always confident

  • #147935
    Basically both are practically same. Functions will returns a value but method will not. Both can work on global data. Data which is available to the entire programming. Function is a piece of code that performs a single task or job. If you add two numbers together that is called a function. A function is a named block of code that can be invoked from anywhere within the code. You can pass arguments as parameters and assign the return value to a variable. Method is a part of class, object, code that you can use to stop writing the same code again and again. Method imparts interface that other classes use to access and modify the data properties of any data object.

  • #147939
    Function and method are similar in that both are blocks/pieces of codes, both have names specific to each method or function.

    The fine difference would be in that, a function is independent of an object but a method is often based on a object or class. Methods are codes that are defined inside and a part of a class but functions are defined outside of class.

    So, technically all methods are functions in true sense, but the reverse (all functions are not methods) is not true. Methods are defined in Object Oriented language whereas functions are defined as structural language.

    The difference may be appreciable if we consider the language we are referring to. For instance in C, it is mostly functions and in Java or C++ it is only methods.

  • #147948
    In an object oriented programing (OOP) any procedure associated with the object class is known as a method. A class can have more than one method. Method can also be thought as an interface between the class and outside world.

    Function is a term used in a narrow prospect. A function is more near to a subroutine and return a value depending upon its argument.

    Long back only subroutine or function were used in the computer jargon. With development of languages dedicated to object oriented programming the 'method' took over as the leading jargon for addressable functions and subroutines.

    Knowledge is power.


  • Sign In to post your comments