473,389 Members | 1,142 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,389 software developers and data experts.

Calling a function when passing no parameters

If I have the following function:

function foo(){

alert('hi');

}

and I don't need to pass any parameters to it, is calling it this way:

foo;

the same as calling it this way:

foo();
Jul 17 '08 #1
8 2915
Yansky <th*********@gmail.comwrites:
If I have the following function:

function foo(){

alert('hi');

}

and I don't need to pass any parameters to it, is calling it this way:

foo;

the same as calling it this way:

foo();
No. foo; does not call it at all. It's a statement returning the
function foo, but since you're not doing anything with the value it's
a useless statement.

A contrived example of foo; is:

var bar = foo; // assign function foo to bar
bar(); // call the function.

IOW, you do need the parentheses if you want to call a function.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 17 '08 #2
On Jul 18, 8:12*am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Yansky <thegoodd...@gmail.comwrites:
If I have the following function:
function foo(){
* alert('hi');
}
and I don't need to pass any parameters to it, is calling it this way:
foo;
the same as calling it this way:
foo();

No. foo; does not call it at all. It's a statement returning the
function foo, but since you're not doing anything with the value it's
a useless statement.

A contrived example of foo; is:

var bar = foo; // assign function foo to bar
bar(); *// call the function.

IOW, you do need the parentheses if you want to call a function.

--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Thanks for clearing that up. :)
Jul 17 '08 #3
On Jul 18, 8:40*am, Yansky <thegoodd...@gmail.comwrote:
On Jul 18, 8:12*am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Yansky <thegoodd...@gmail.comwrites:
If I have the following function:
function foo(){
* alert('hi');
}
and I don't need to pass any parameters to it, is calling it this way:
foo;
the same as calling it this way:
foo();
No. foo; does not call it at all. It's a statement returning the
function foo, but since you're not doing anything with the value it's
a useless statement.
A contrived example of foo; is:
var bar = foo; // assign function foo to bar
bar(); *// call the function.
IOW, you do need the parentheses if you want to call a function.
--
Joost Diepenmaat | blog:http://joost.zeekat.nl/|work:http://zeekat.nl/

Thanks for clearing that up. :)
Oh wait I have one more question. What about when you're calling the
function inside an event listener? e.g.
function foo(){

alert('hi');

}
window.addEventListener("load", foo, false);
Jul 17 '08 #4
Yansky wrote:
On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
>Yansky <thegoodd...@gmail.comwrites:
>>If I have the following function:
function foo(){
alert('hi');
}
and I don't need to pass any parameters to it, is calling it this way:
foo;
the same as calling it this way:
foo();
That's probably the second most stupid question I have ever read on Usenet.
>No. foo; does not call it at all. It's a statement returning the
function foo, but since you're not doing anything with the value it's
a useless statement.

A contrived example of foo; is:

var bar = foo; // assign function foo to bar
bar(); // call the function.

IOW, you do need the parentheses if you want to call a function.
[...]

Thanks for clearing that up. :)
I really wonder what is so difficult about trying something out before
asking (or to read the available reference material, including this
newsgroup's FAQ, for that matter). If you are this afraid that code that
you write could have effects this dangerous on the computer you are
developing on, you should forget all about programming and computer science,
and move to a happy little island nowhere near any technology.

<http://jibbering.com/faq/>
<http://catb.org/~esr/faqs/smart-questions.html>
PointedEars, shaking his head
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 17 '08 #5
Yansky wrote:
Oh wait I have one more question.
You don't say.
What about when you're calling the function inside an event listener?
e.g.
function foo(){

alert('hi');

}
window.addEventListener("load", foo, false);
`foo' refers to the (event) listener, which is the user-defined function.
Again, the function is _not_ called here, but being referred to. It is
called *later*, by the DOM implementation's event handler, using the passed
Function object reference.

RTFM. RTFFAQ. STFW.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jul 17 '08 #6
On Jul 18, 8:50*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Yansky wrote:
On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Yansky <thegoodd...@gmail.comwrites:
If I have the following function:
function foo(){
* alert('hi');
}
and I don't need to pass any parameters to it, is calling it this way:
foo;
the same as calling it this way:
foo();

That's probably the second most stupid question I have ever read on Usenet.
w00t!
Jul 17 '08 #7
Yansky wrote:
Thomas 'PointedEars' Lahn wrote:
>Yansky wrote:
>>On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Yansky <thegoodd...@gmail.comwrites:
If I have the following function:
function foo(){
alert('hi');
}
and I don't need to pass any parameters to it, is calling it this way:
foo;
the same as calling it this way:
foo();
That's probably the second most stupid question I have ever read on Usenet.

w00t!
And that's definitely the most stupid reply I have ever read.

Congratulations. Don't you want to apply for the Darwin Award as well?
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 17 '08 #8
Yansky wrote:
Oh wait I have one more question. What about when you're calling the
function inside an event listener? e.g.
function foo(){
alert('hi');
}
window.addEventListener("load", foo, false);
That's fine, you're not calling it, you're nominating it as the function
that should be called.

Here, alias will be set to 3;

function foo(){return 3;};
var alias=foo();

Here, alias will become an actual alias of the function foo.

function foo(){return 3;}
var alias=foo;
//you can now do this:
alert(alias()); //alert 3
Jul 17 '08 #9

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

Similar topics

8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
5
by: Zlatko Matić | last post by:
Hello. How can I call some functions on MSDE when working in .mdb ? Especially in-line functions which are similar to stored procedures. How can I use MSDE in-line functions as recordsource for...
30
by: Tim Marshall | last post by:
Here's the scenario, A2003, Jet back end, illustrated with some cut down code at the end of the post: A proc dims a snapshot recordset (dim rst as Dao.recordset) and opens it. There are several...
1
by: pkpatil | last post by:
Hi, I recently saw a code with a mix of C & C++, wherein a C++ file was calling a C function dropping the last argument. The code was similar to below:...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: rocketfire97 | last post by:
I'm trying to call a COM object using C# but having no luck getting values back for passed in ref objects. I've tried the same call using VB.NET and can get data back. How would I implement the...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.