Question:
Passing a function through parameters with parameters.?
Philip T
2010-10-01 00:24:30 UTC
Okay, so I need to pass a function through a parameter. Here's an example that is pretty close to what I want to do only that the function I'm passing has parameters in it too.
// returns the current hour
function GetCurrentHour()
{
// obtaining the current hour
var date = new Date();
var hour = date.getHours();

// return the hour
return hour;
}

// display greeting
function DisplayGreeting(hourFunc)
{
// retrieve the hour using the function received as parameter
hour = hourFunc();

// display greeting
if (hour >= 22 || hour <= 5)
document.write("Goodnight, world!");
else
document.write("Hello, world!");
}

// call DisplayGreeting
DisplayGreeting(GetCurrentHour);


Any suggestions??
Four answers:
Gardner
2010-10-01 00:31:14 UTC
MyFunction1(MyFunction2(Parm1, Parm2));
?
2010-10-01 00:32:06 UTC
Please remember to write which language your using so that people answering can match the syntax.



for a start you can not pass a function as a value to another function. you can only pass varibles.



your function getcurrenthours returns a varible of type hours, so dispaly greeting shoud display



so your doce should read something abit more like this:



//returns the current hour in a integer

function int GetCurrentHour()

{

// obtaining the current hour

var date = new Date();

int hour = date.getHours();



// return the hour

return hour;

}



// display greeting

function DisplayGreeting(int hour)

{



// display greeting

if (hour >= 22 || hour <= 5)

document.write("Goodnight, world!");

else

document.write("Hello, world!");

}



//your command

// call DisplayGreeting

DisplayGreeting(GetCurrentHour());



good luck
BobberKnob
2010-10-01 00:44:22 UTC
Why not just call your function inside of the other?
?
2016-11-16 08:52:28 UTC
ok..first you got comprehend like the place the sector is..you comprehend the place the two cagesa re and the boats interior the middle yet be careful via dogs on the factors so which you may bypass around like the image around image...wish this helps


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...