Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling vs invoking a function

Tags:

javascript

Up to this point, I thought "calling" and "invoking" a function meant the same thing. However, in a YouTube tutorial it said to invoke a function by calling it. My first thought was that the wording was a mistake, but on W3Schools' page on Function Invocation, it says:

It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.

Okay, so there's a difference. What is it?

like image 458
nCardot Avatar asked Jun 16 '18 04:06

nCardot


People also ask

What does it mean to call or invoke a method?

When you execute the method in your code,directly, it's called Calling. When someone else executes it for you, it's Invoking.

What is invoking of a function?

Invoking a JavaScript Function The code inside a function is not executed when the function is defined. The code inside a function is executed when the function is invoked. It is common to use the term "call a function" instead of "invoke a function".

What is the difference between calling and declaring a function?

declare and define are the same, and they mean when you write all the code for your function. At that point the function just sits there doing nothing. call is when you tell the JavaScript interpreter to run the code in your function.

What does it mean to invoke a program?

The process of activating. The term invoke is usually used to refer to a routine or function in a program. Call and invoke are synonymous terms in this sense.


4 Answers

Your reference text:

It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.

Now let me rephrase it:

It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use the term invoke instead of call, because a JavaScript function can be invoked indirectly like fn.call() and fn.apply() without being called directly like fn().

So, when I do fn(), it's invoked directly and when I do it like fn.call(), it's invoked indirectly but in both cases, the function is being invoked. Otherwise, I see no difference here and I can also say that I can call a function in many ways, for example:

fn(); // I'm calling it
fn.call(); // I'm calling it
fn.apply(); // I'm calling it

So, the difference is semantic but both are interchangeable, IMO. BTW, I wrote a comment above, under the question and I would like to put it here, which is:

IMO, that's a misleading statement. Maybe there are some indication of call/apply or something else but it's totally confusing.

like image 194
The Alpha Avatar answered Oct 19 '22 03:10

The Alpha


The difference is semantic and subtle. When you call a function, you are directly telling it to run. When you invoke a function, you are letting something run it.

There is one way to call a function:

myFunction()

Here, you are invoking the function (letting it run) by calling it directly.

There are many ways to invoke a function (given throughout different comments and answers). Here's one example:

function invoker(functionName) {
  functionName() 
}

invoker(myFunction)

Here, by calling invoker, you are invoking myFunction, which is being called indirectly.

like image 42
Brett Commandeur Avatar answered Oct 19 '22 02:10

Brett Commandeur


Yes, in most cases we use both to refer the execution of a function.

There are 2 ways to reach place B from your HOME.

  1. Direct/Automatic way (Invoke), i.e. if you choose first way, you do not need to walk. Someone will automatically take you to place B.
  2. Indirect way (Call), i.e. if choose second way, you only need to reach A(by walk). Someone is there at place A to automatically take you to place B.

Have a look at the below image. I think it will clear your doubt.

In case of Calling, you originally refer to the statement that actually calls the function.

In case of Invoking, you indirectly refer to calling statement to actually invoke/run the function.

like image 5
hygull Avatar answered Oct 19 '22 01:10

hygull


Many people use the term calling and invoking interchangeably but that's not right. There is a very slight difference between calling and invoking a function. In JavaScript functions can be invoked without being called which means that the code inside the body of the function can be executed without creating an object for the same. It is tied to the global object. When there is no individual object, the value of this is associated with the global object.

There is also a difference between call and apply, the fundamental difference is that call() accepts an argument list, while apply() accepts a single array of arguments. A different this object can be assigned when calling an existing function. this refers to the current object, the calling object. With call, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

So, the major difference between invoking and calling comes in terms of the this object. Calling let's you set the this value whereas invoking just ties it to the global object.

like image 3
Nirmal Dalmia Avatar answered Oct 19 '22 03:10

Nirmal Dalmia