Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Responding to User Interaction Functions as Parameters

Why do we have to write the part func(arg) inside the function.?

Why do we have to write the part func(arg) inside the function.?

function weird(func,arg) {
func(arg)     //Why do we use this over here
  }

weird(function(something) {
  console.log(something)}, "Greetings Everyone"
);

3 Answers

Tobiasz Gala
seal-mask
.a{fill-rule:evenodd;}techdegree
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,517 Points

The point here is to understand that functions can be sent to other functions

function weird(func,arg) { // look at parameters - one is a function a one is a string
                        //In other words you have a function which needs a function and argument

    func(arg)     // here you call that function which was send as an argument function 
                    //with a second argument
}

// here you call function an you pass anonymous function (with no name), and your string
weird(function(something) { console.log(something) }, "Greetings Everyone");

Let's change this example without anonymous function

// this function need function and string in order to work
function wierd(func, arg) {
    // you call function which was send as an argument
    // in this example you are sending someName() function
    // and second argument from this function is passed to someName() 
    // which is 'Greetings Everyone'
    func(arg); // equals someName('Greetings Everyone');
}

// this function is called when you call wierd() function
function someName(something) {
    console.log(something);
}

// you call wierd() and pass someName() as first parameter, and string as second
wierd(someName(), 'Greetings Everyone');

Thanks alot Tobiasz for such a detailed answer. I got much of the concept . But can you please tell me that why do we really need " func(arg) " .??? What if we leave it blank or do it like this:

function weird(func,arg){ 
function Goofy (something){console.log(something)} }

weird(Goofy(), "Greetings Everyone" );

And your Example without anonymous function does not works. Can you please tell why.?

Tobiasz Gala
seal-mask
.a{fill-rule:evenodd;}techdegree
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,517 Points
[...]

// you call wierd() and pass someName() as first parameter, and string as second
wierd(someName, 'Greetings Everyone'); // mistake here, parentheses are used to call a function you don't use it when you pass its reference

Sorry made one mistake in my example. It is explained in the comment