473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

(simple) question - function calls without ()

ed
Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?

Thanks!

eddie

Jun 17 '07 #1
12 1462
ed said the following on 6/17/2007 10:15 AM:
Hi there,

I just remembered this from a few days ago, and was looking for a reason
for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?
When you want the function executed right then. Or, the function you are
calling expects the () to be there. With the (), the function executes
then and what your above function call would end up as would be
something like this:

addEvent( whatever, whatever, returnFromfunctionName );

Without the () you are simply passing the name of a function.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 17 '07 #2
Randy Webb wrote:
ed said the following on 6/17/2007 10:15 AM:
>Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?

When you want the function executed right then.

Or, the function you are calling expects the () to be there.
I don't understand this, could you explain this a little further? Or
give me an example?

I have no clue how the function would "expect" it to be there, or even
be able to tell "how" it was called.
With the (), the function executes
then and what your above function call would end up as would be
something like this:

addEvent( whatever, whatever, returnFromfunctionName );

Without the () you are simply passing the name of a function.
Thank you (in advance)!

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 17 '07 #3
Randy Webb wrote:
ed said the following on 6/17/2007 10:15 AM:
>Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?

When you want the function executed right then. Or, the function you are
calling expects the () to be there. With the (), the function executes
then and what your above function call would end up as would be
something like this:

addEvent( whatever, whatever, returnFromfunctionName );

Without the () you are simply passing the name of a function.
Not the name of the function, but the function itself.
Jun 17 '07 #4
On Jun 17, 9:44 am, -Lost <maventheextrawo...@techie.comwrote:
Randy Webb wrote:
ed said the following on 6/17/2007 10:15 AM:
Hi there,
I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:
addEvent( whatever, whatever, functionName() );
and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?
When you want the function executed right then.
Or, the function you are calling expects the () to be there.

I don't understand this, could you explain this a little further? Or
give me an example?
I second this request (but I'm guessing it was loose wording.)

Peter

Jun 17 '07 #5
Douglas Crockford said the following on 6/17/2007 1:23 PM:
Randy Webb wrote:
>ed said the following on 6/17/2007 10:15 AM:
>>Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of
them, and it worked. What is the reasoning behind this? When are
you supposed to add the () ?

When you want the function executed right then. Or, the function you
are calling expects the () to be there. With the (), the function
executes then and what your above function call would end up as would
be something like this:

addEvent( whatever, whatever, returnFromfunctionName );

Without the () you are simply passing the name of a function.

Not the name of the function, but the function itself.
That would depend on how it was called and what was expected. I don't
think that setTimeout (or addEvent) is getting passed the entire
function, just a name reference to it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 17 '07 #6
-Lost said the following on 6/17/2007 12:44 PM:
Randy Webb wrote:
>ed said the following on 6/17/2007 10:15 AM:
>>Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of
them, and it worked. What is the reasoning behind this? When are
you supposed to add the () ?

When you want the function executed right then.
>
Or, the function you are calling expects the () to be there.

I don't understand this, could you explain this a little further? Or
give me an example?

I have no clue how the function would "expect" it to be there, or even
be able to tell "how" it was called.
Think about setTimeout and this thread:

<URL:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/47698950116c9e84/76177fa1a14eb9a1?lnk=gst&q=settimeout+randy+webb&r num=8#76177fa1a14eb9a1>
Make sure you read the last post by me in that thread as my first post
with 4 functions had an error in it that is covered in the very last post.

A call to setTimeout wants () in some instances and not in others.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 17 '07 #7
On Jun 17, 11:10 am, Randy Webb <HikksNotAtH...@aol.comwrote:
-Lost said the following on 6/17/2007 12:44 PM:
Randy Webb wrote:
ed said the following on 6/17/2007 10:15 AM:
Hi there,
>I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:
>addEvent( whatever, whatever, functionName() );
>and as I recall it didn't like the parenthesis (). I got rid of
them, and it worked. What is the reasoning behind this? When are
you supposed to add the () ?
When you want the function executed right then.
Or, the function you are calling expects the () to be there.
I don't understand this, could you explain this a little further? Or
give me an example?
I have no clue how the function would "expect" it to be there, or even
be able to tell "how" it was called.

Think about setTimeout and this thread:

<URL:http://groups.google.com/group/comp.lang.javascript/browse_frm/thread...>
Make sure you read the last post by me in that thread as my first post
with 4 functions had an error in it that is covered in the very last post.

A call to setTimeout wants () in some instances and not in others.

A slightly odd example since the () are inside a string so setTimeout
doesn't actually see the (). It just sees a string that will be
eval'ed later. Calling setTimeout with a string is akin to eval()
which we all know is a sin.

Peter

Jun 17 '07 #8
On Jun 17, 10:54 am, Randy Webb <HikksNotAtH...@aol.comwrote:
Douglas Crockford said the following on 6/17/2007 1:23 PM:
Randy Webb wrote:
ed said the following on 6/17/2007 10:15 AM:
Hi there,
>I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:
>addEvent( whatever, whatever, functionName() );
>and as I recall it didn't like the parenthesis (). I got rid of
them, and it worked. What is the reasoning behind this? When are
you supposed to add the () ?
When you want the function executed right then. Or, the function you
are calling expects the () to be there. With the (), the function
executes then and what your above function call would end up as would
be something like this:
addEvent( whatever, whatever, returnFromfunctionName );
Without the () you are simply passing the name of a function.
Not the name of the function, but the function itself.

That would depend on how it was called and what was expected. I don't
think that setTimeout (or addEvent) is getting passed the entire
function, just a name reference to it.
eh?

var foo = function() {
console.log('foo');
};
setTimeout(foo,10);
foo = function() {
console.log('foobar');
};

The output of the above is "foo" because setTimeout is passed the
first function (not it's name)
var foo = function() {
console.log('foo');
};
setTimeout("foo()",10);
foo = function() {
console.log('foobar');
};

The output of the above is "foobar" because the string "foo()" is
evaluated when the timeout expires. However setTimeout is being passed
a string in this case and knows nothing about the ()

Anyway, I think it would be better to write this example as.

var foo = function() {
console.log('foo');
};
setTimeout(function(){foo();},10);
foo = function() {
console.log('foobar');
};

which outputs "foobar" since foo has been redefined by the time the
timeout expires.

Peter

Jun 17 '07 #9
ed
On 2007-06-17 14:43:32 -0400, Peter Michaux <pe**********@gmail.comsaid:
On Jun 17, 10:54 am, Randy Webb <HikksNotAtH...@aol.comwrote:
>Douglas Crockford said the following on 6/17/2007 1:23 PM:
>>Randy Webb wrote:
ed said the following on 6/17/2007 10:15 AM:
Hi there,
>>>>I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:
>>>>addEvent( whatever, whatever, functionName() );
>>>>and as I recall it didn't like the parenthesis (). I got rid of
them, and it worked. What is the reasoning behind this? When are
you supposed to add the () ?
>>>When you want the function executed right then. Or, the function you
are calling expects the () to be there. With the (), the function
executes then and what your above function call would end up as would
be something like this:
>>>addEvent( whatever, whatever, returnFromfunctionName );
>>>Without the () you are simply passing the name of a function.
>>Not the name of the function, but the function itself.

That would depend on how it was called and what was expected. I don't
think that setTimeout (or addEvent) is getting passed the entire
function, just a name reference to it.

eh?

var foo = function() {
console.log('foo');
};
setTimeout(foo,10);
foo = function() {
console.log('foobar');
};

The output of the above is "foo" because setTimeout is passed the
first function (not it's name)
var foo = function() {
console.log('foo');
};
setTimeout("foo()",10);
foo = function() {
console.log('foobar');
};

The output of the above is "foobar" because the string "foo()" is
evaluated when the timeout expires. However setTimeout is being passed
a string in this case and knows nothing about the ()

Anyway, I think it would be better to write this example as.

var foo = function() {
console.log('foo');
};I
setTimeout(function(){foo();},10);
foo = function() {
console.log('foobar');
};

which outputs "foobar" since foo has been redefined by the time the
timeout expires.

Peter
Wow, that's really interesting. Thanks for the explanation. One
question; in the secont mini-example, what causes the string "foo()"
to be evaluated? Is that a specification of setTimeout, or a result of
some other language concept? I haven't yet seen string evaluation in
my studies.
-e

Jun 17 '07 #10
On Jun 17, 1:43 pm, ed <u...@superhoost.comwrote:
On 2007-06-17 14:43:32 -0400, Peter Michaux <petermich...@gmail.comsaid:
On Jun 17, 10:54 am, Randy Webb <HikksNotAtH...@aol.comwrote:
Douglas Crockford said the following on 6/17/2007 1:23 PM:
>Randy Webb wrote:
ed said the following on 6/17/2007 10:15 AM:
Hi there,
>>>I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:
>>>addEvent( whatever, whatever, functionName() );
>>>and as I recall it didn't like the parenthesis (). I got rid of
them, and it worked. What is the reasoning behind this? When are
you supposed to add the () ?
>>When you want the function executed right then. Or, the function you
are calling expects the () to be there. With the (), the function
executes then and what your above function call would end up as would
be something like this:
>>addEvent( whatever, whatever, returnFromfunctionName );
>>Without the () you are simply passing the name of a function.
>Not the name of the function, but the function itself.
That would depend on how it was called and what was expected. I don't
think that setTimeout (or addEvent) is getting passed the entire
function, just a name reference to it.
eh?
var foo = function() {
console.log('foo');
};
setTimeout(foo,10);
foo = function() {
console.log('foobar');
};
The output of the above is "foo" because setTimeout is passed the
first function (not it's name)
var foo = function() {
console.log('foo');
};
setTimeout("foo()",10);
foo = function() {
console.log('foobar');
};
The output of the above is "foobar" because the string "foo()" is
evaluated when the timeout expires. However setTimeout is being passed
a string in this case and knows nothing about the ()
Anyway, I think it would be better to write this example as.
var foo = function() {
console.log('foo');
};I
setTimeout(function(){foo();},10);
foo = function() {
console.log('foobar');
};
which outputs "foobar" since foo has been redefined by the time the
timeout expires.
Peter

Wow, that's really interesting. Thanks for the explanation. One
question; in the secont mini-example, what causes the string "foo()"
to be evaluated? Is that a specification of setTimeout, or a result of
some other language concept? I haven't yet seen string evaluation in
my studies.
setTimeout can take a string or function as it's first argument. I
believe the string was first and the function option was added later.
Anyway, it must be setTimeout that actually calls eval if the argument
was a string when the timeout expires.

<URL: http://developer.mozilla.org/en/docs/DOM:window.setTimeout>

This isn't really a language-related issue. The setTimeout function is
not part of the actual language and so not in ECMAScript
specification. This function is a property of the window object that
the browser provides and that your code can use. You can write
window.setTimeout() to specifically show that you are calling a
property of the window object.

Peter

Jun 17 '07 #11
In comp.lang.javascript message <2007061710153427544-user@superhoostcom>
, Sun, 17 Jun 2007 10:15:34, ed <us**@superhoost.composted:
>Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?
Your functionName is, presumably, the name of a function, and a function
is a "recipe" for possible action. There is a clear distinction in
cooking between a recipe, which can be published, and the result of
executing it, which can be eaten.

Passing functionName is equivalent to passing "Cake recipe book page
35". Passing functionName() is equivalent to passing a cake resulting
from using page 35.

The parentheses () - which need not be contiguous - are an operator
which, when postfixed to a function identification (name, expression,
etc.), cause the function to be executed - the function is given the
contents of the parenthesis pair as data or, for cake, variable
ingredients.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 18 '07 #12
Dr J R Stockton wrote:
In comp.lang.javascript message <2007061710153427544-user@superhoostcom>
, Sun, 17 Jun 2007 10:15:34, ed <us**@superhoost.composted:
>Hi there,

I just remembered this from a few days ago, and was looking for a
reason for it. I was trying to call a function as so:

addEvent( whatever, whatever, functionName() );

and as I recall it didn't like the parenthesis (). I got rid of them,
and it worked. What is the reasoning behind this? When are you
supposed to add the () ?

Your functionName is, presumably, the name of a function, and a function
is a "recipe" for possible action. There is a clear distinction in
cooking between a recipe, which can be published, and the result of
executing it, which can be eaten.

Passing functionName is equivalent to passing "Cake recipe book page
35". Passing functionName() is equivalent to passing a cake resulting
from using page 35.

The parentheses () - which need not be contiguous - are an operator
which, when postfixed to a function identification (name, expression,
etc.), cause the function to be executed - the function is given the
contents of the parenthesis pair as data or, for cake, variable
ingredients.
I am confused. Did you just bake a cake or answer the originally posted
question?
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
Hrmmm... I checked, and it says nothing about cakes, recipes, or
ingredients.

Perhaps you could elaborate without the use of your "bakery?" :D

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 19 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
13
by: lost hope | last post by:
hi, i have a very simple html page with a global variable, a function to increment its value, and a form that calls the function. however, what happens is that every time i click the image, the...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
6
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: ...
8
by: Jon Harrop | last post by:
I am trying to learn C# and .NET programming in general but I am finding it very hard going. To start with, I'd like to translate some trivial functions from other languages that I am familiar with...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.