Function with parenthesis and without parenthesis


#1

When we pass a reference we use without () and when we are returning a function we use functionName(). is that correct.

also if the function does not return any value. what does it indicate.

In the below example, i was testing why functionName() is logging // 10, 3, 6, 9 and without () is logging 3,6,9,10

Sample:

var numbers = [3, 10, 9 , 6];

function compareNums(a, b){
return a - b;
}

numbers.sort(compareNums); // 3, 6, 9, 10
numbers.sort(compareNums()); // 10, 3, 6, 9

console.log(numbers);

`


#2

This line isn't actually using the function compareNums, because, with the parenthesis, it is calling the function without arguments, whereas, without them, it gives the function to the .sort function. This means that numbers.sort(compareNums) it the same as

numbers.sort(function (a, b){
 return a - b;
})

But, numbers.sort(compareNums()) is the same as numbers.sort(undefined), because you gave it no arguments to work with, so it sorts the default way,


#3

Thanks.

So sort method converts number to strings. So unless we mention the compareFunc them it takes the Unicode code point order.

What is the diff when we say the method is used as a reference and not returned anything

What are we storing in the variables. I have seen in many JS code samples as var being used like this.

var x = compareNums;
console.log(x); // [Function: compareNums]

var y = compareNums();
console.log(y); // NaN


#4

Re this part of your question:

I think so...

Peter Bailey writes on stackoverflow:

I'll use a simple multiply function to demo instead:

function Multiply(operator, operand) {
    return operator * operand;
}

This could equally be written:

 Multiply = function(operator, operand) {
     return operator * operand;
 }

While in the first example, the implication may not be obvious, the second example shows more clearly that we're assigning a function which has 2 parameters to a variable called Multiply [...] functions are "first class citizens", that is, they can be passed around exactly as if we were passing around values.

So now to the difference of assignment:

var operator = 3;
var operand = 4;
var ret = Multiply(operator, operand);

At the point of defining the ret variable, Multiply is executed and the return value is assigned - ret becomes equal to 12.

Lets try that again a different way:

var operator = 3;
var operand = 4;
var ret = Multiply;

Now, at the point of defining ret, ret becomes your Multiply function as opposed to being the result obtained from your Multiply function. Calls to ret() will cause your Multiply function to be executed, and you can call it exactly as if you'd called Multiply(operator, operand):


Apr 21 update: May I suggest to you this mini lesson on "closures" I tried this morning? Some of it is still confusing to me as well, but I found it helpful to better internalize how functions "work"; maybe you will like it too :slight_smile:


#5

I don't understand what you're asking here.


#6

I will frame in a better manner.
By default, the array sort() method sorts alphabetically ascending. So unless we mention the compare() them it takes the Unicode code point order.

You said: This means that numbers.sort(compareNums) it the same as

numbers.sort(function (a, b){
return a - b;
})

numbers.sort(compareNums); // 3, 6, 9, 10

Let me give some values
var numbers = [3, 10, 9 , 6];

compare(3,10) // - 7
compare(6, 10) // -4
compare(10, 6) // 4

how are this value getting passed in the numbers.sort(compareNums) function


#7

I will frame in a better manner

array.sort() will sort strings alphabetically ascending. So unless we mention the compare() inside the sort() them it takes the Unicode code point order.

You said this
numbers.sort(compareNums) it the same as

numbers.sort(function (a, b){
return a - b;
})

let me take some values
compareNums(3,6) // -3
compareNums(6, 10) // -4
compareNums(10, 9) // 1

If the return value is negative, it means that A appears before B in the sorted sequence. If the return value is 0, then A and B have the same sort order. If the return value is positive, it means that A appears after B in the sorted sequence.

Is this the standard protocol for JS wrt to compare function with numbers?


#8

The .sort function goes through all the pairs,

3, 10
10, 9
3, 9
10, 6
9, 6
3, 6

avoiding the reverse (i.e. if it does 3, 10 it won't do 10, 3). It sends each of these pairs through your compareNums function and sorts based on what it returns.

Yes.


#9
var makeCounter = function ()
{
var count, f;
count = 0;
f = function () {
    count = count + 1;
    return count;
};
return f;
};
var counter = makeCounter();
console.log(counter); // gives [Function]

why is this counter not returning the value. Since we are using makeCounter() and function is returning a value, should it not get assigned to var counter?

var a = counter();
var b = counter();
console.log(a); // gives 1
console.log(b); // gives 2

why is the return value working now.

can you also explain use and functionality of closure in easy to understand language.


#10

The line "return f;" returns the function stored in f, rather than the count variable, because you didn't call the function. If you want to execute the code inside a function then you need to use the parenthesis (return f(); ).

Because you called it with the parenthesis, and the counter variable was a clone of the function f.

Meaning the {} in a function?


#11

when and in what cases do we use cliosures an example to showcase

and why it is such an imp topic in JS after Prototype inheritance.


#12

Try reading this


#13

when you are saying

function add(a, b) {
 //return something
}

var n = function add(a,b) {
//return something
}

Is the first function refers to global object (window) and second function stores value in var. Does that become local or function expression

Can you kindly elaborate


#14

function add(a, b) {
 //return something
}

var add = function (a,b) {
//return something
}

These are two different ways of doing the same thing. Your line var n = function add(a,b) { is improper syntax and would cause an error. To store a value that the function returns, your code should look like this...

function add(a, b) {
 //return something
}

var n = add("some", "thing");