Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEduard Edwin
14,569 PointsPassing a function directly as an argument / parameter to another function
Can I passed a function directly as an argument / parameter to another function like the example below:
function calculateSomething (getRandomNumber()) {
// Do some calculation and return it
}
Without specifying the parameter name, can this be done?
1 Answer
Michael Liendo
15,326 PointsSort of. Since your defining a function---by using the "function" keyword, you want to give your parameter a value like int/string, bool, array, object. You can pass in a called function, but it should resolve to one of those types.
So in the end, your function should like look:
function calculateSomething(randomNumber) { //notice I'm not calling randomNumber, just naming the parameter
//do some calculation with the random number
}
and then call the function like:
// here I'm calling calculateSomething and calling it with function that will resolve to a number
calculateSomething(randomNumber());