oop - Method, Function, Operation, Procedure, Subroutine whats the exact difference? -


are there official definitions these terms in oop? or have evolved on time , depending on former computer science education (or age) use 1 or other?

so far found definitions of method vs. function:

difference between method , function

a function piece of code called name. ... data passed function explicitly passed.

a method piece of code called name associated object.

and function vs. procedure:

what difference between "function" , "procedure"?

a function returns value , procedure executes commands.

a procedure set of command can executed in order.

in programming languages, functions can have set of commands. hence difference in returning value part.

even if there subtle differences in definition authors main aspect seems be: method operates on object in contrast function gets data passed parameters. if function not return value called procedure.

but how subroutine , operation linked these terms?

edit: since seems too broad here attempt narrow down: method, procedure , function pretty clear former research. subroutine not vague anymore.

so question is: operation in field of computer science?

the following understanding. note these "soft" definitions - there no official definition of of these, , exact meaning may vary between languages. (for example, "static methods" in java , c# don't fit definition of "method", unless consider classes objects)

  • a subroutine "some code can call whenever". it's literally routine called other routines (hence sub-routine). "subroutine" , "routine" seem quite archaic terms.

    think of basic's gosub instruction: (never mind basic terrible language)

     10 print "hello"  20 gosub 100  30 print "goodbye"  40 gosub 100  50 end  100 print "world" 110 return 

    this prints:

    hello world goodbye world 
  • a procedure same thing subroutine, less archaic.

  • a function procedure, instead of being list of commands, can take parameters , return value. should familiar functions many languages, here's example in c++:

    #include <iostream> using namespace std;  int calculate(int a, int b) {     return + b - 2; }  int main() {     cout << calculate(5, 2) << endl;     cout << calculate(100, 20) << endl;      int = calculate(5, 1);     for(int k = 0; k < i; k++) // repeat next line times         cout << "hello" << endl; } 

    this prints:

    1 78 hello hello 

    notice function calculate doesn't printing itself - instead, returns number main can choose print, or else with. third call calculate returns 2, 2 isn't printed - instead, determines how many times "hello" printed.

    a function no arguments, returns nothing, equivalent procedure (or subroutine).

  • a method function can called on object. might familiar non-static methods java or c#. method has access state of object called on (which called receiver).

    unlike functions, methods polymorphic on type of receiver - it's not possible directly see sequence of commands run when make method call.

    a simple example of methods in java, demonstrating polymorphism:

    class car {     int getnumberofwheels() {return 4;}     void printnumberofwheels() {         system.out.println(getnumberofwheels());     } }  class hondacivic extends car {     // no code here }  class reliantrobin extends car {     int getnumberofwheels() {return 3;} }  class main {     public static void main(string[] args) {         car h = new hondacivic();         car r = new reliantrobin();          h.printnumberofwheels();         r.printnumberofwheels();     } } 

    this prints:

    4 3 

    even though might think printnumberofwheels prints 4 - subclasses can "intercept" getnumberofwheels method call.


Popular posts from this blog