What's the difference between “call” and “invo

2019-01-30 11:30发布

I'm currently reading a book by Daniel M. Solis called "Illustrated C# 2010." The book says:

"When a method is called or invoked ..."

What is the difference between these two terms?

4条回答
唯我独甜
2楼-- · 2019-01-30 12:10

From my research (personal & unpaid), looking at the common way these terms are used in programming literature & "in the wild", I have found that these definitions seem to fit their usages.

Execution refers to the process of running code. Exact method does not matter, can be compiled or not, done by a computer or not.

Applying/Application refers to the binding of arguments to the function. Application can be both partial and complete. From functional programming world, partial application produces another function with less parameters while complete application produces a thunk. Thunks are functions with no parameters and can help with "lazy evaluation".

Invoking/Invocation refers to the process required to schedule the function, with its fully bound arguments, for execution. Such systems include pushing arguments onto the stack and transferring the PC to the new address, placing messages/objects/functions/thunks on a queue for later execution or various other RPC systems. Exact mechanism does not matter. The notion of scheduling for future execution matters. Invoking requires that the will function execute.

Calling is the least defined out of the lot. Generally refers to the combined process of fully applying the function then invoking it, usually with the added semantic that your code will wait for a return value.

Please also note that all of these terms are subjective from the point view of the current code that is being written. Invoking a function via a RPC call is only invoking it from the client's side. From the server's side the request has a different invocation point, if the function even has any "meaning" as a function on the server's side.

查看更多
beautiful°
3楼-- · 2019-01-30 12:11

Function calling is when you call a function yourself in a program. While function invoking is when it gets called automatically.

For example, consider this program:

struct s
{
  int a,b,s;

  s()
  {
    a=2;
    b=3;
  }

  void sum()
  {
    s=a+b;
  }
};

void main()
{
  struct s obj; //line 1
  obj.sum(); // line 2
}

Here, when line 1 is executed, the function (constructor, i.e. s) is invoked. When line 2 is executed, the function sum is called.

source: web

查看更多
在下西门庆
4楼-- · 2019-01-30 12:25

Method Invokation is a term usually refered to indirectly calling a method(function) because of problems or difficulties in calling it directly.

For example in the context of Parallel programming:Consider two threads inside one application space are running parallely. Calling a public method of an object residing on aother thread throws Cross Thread Invokation Exception because race may occure. The solution is invoking the object to execute the method and yeild the rest of job to the object to manage parallel requests.

Another example is when you have a delegate pointing to a method somewhere. When you ask the delegate to call that (unknown) method, you Invoke the method to run.

查看更多
Root(大扎)
5楼-- · 2019-01-30 12:26

Maybe he simply considers the terms "call" and "invoke" synonymous, and just wants to mention both words because both terms can be encounter in the wild. Wouldn't it be possible to use or in that case?

查看更多
登录 后发表回答