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 trial

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,529 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,529 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