Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a function call and function reference?

I have the following function

function hello() {  alert("hi!"); } 

Take this piece of code:

var elem = document.getElementById("btn"); elem.onclick = hello; 

My question might be a bit hard to understand, so bear with me: What EXACTLY differentiates THIS piece of code from a normal call, or what makes this piece of code require a reference to the function variable rather than a regular call? (hello();)

How can I know where I'm supposed to give a reference to the function, and when I'm supposed to actually call it?

like image 864
frrlod Avatar asked Apr 08 '13 18:04

frrlod


People also ask

What is a function reference?

The reference is to the procedure identified by the binding with the same name as the selected specific binding, in the dynamic type of the data-ref. Execution of a function reference must not alter the value of any other data item within the statement in which the function reference appears.

What is the difference between function call and function reference?

Notice how both of them are references to functions, not calling. When something expects a reference, you don't call it...you assign a reference to it (first example). When you want to specifically call a function, you call it with () (second example).

What is difference between call by value and call by reference for functions?

In the Call by Value method, there is no modification in the original value. In the Call by Reference method, there is a modification in the original value. In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument.

What do you mean by the call by function or call by reference?

Advertisements. The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.


1 Answers

Well, the onclick property expects a reference to a function, for it to execute when the element is clicked. Normally it's either:

element.onclick = funcRef; 

or

element.onclick = function () {     funcRef(); }; 

(but of course, it's best to use addEventListener and attachEvent)

Notice how both of them are references to functions, not calling.

When something expects a reference, you don't call it...you assign a reference to it (first example).

When you want to specifically call a function, you call it with () (second example). But notice how in the second example, there's still a reference to a function that's assigned to onclick - it's just an anonymous function.

Probably the more important part:

Some people think you want to do this:

element.onclick = funcRef(); 

But that immediately executes the function (because of the ()), and assigns its return value to onclick. Unless the return value is a function, this isn't what you want.

I think the moral of the story is that when you want/need something to execute right now, you call the function. If the function is wanted for later use or needs stored, you don't call it.

like image 83
Ian Avatar answered Sep 22 '22 19:09

Ian