"call a function"?


#1

What is calling a function? I keep getting told to do that on Introduction to Functions in JS, and I look back at my old code to finish the new one but I still don’ tunderstand what calling a function is.( https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/1/3?curriculum_id=506324b3a7dffd00020bf661 )


#2
// the function (callee)

const foo = bar => {
    return bar * 2;
};
// the call expression statement (caller)

console.log(foo(21))    // -> 42

A call is any expression that references a function object AND invokes it.

foo()

Above, foo is the function object, () is the invocation of that function object.

The above example is written in ES6+ syntax. I hadn’t looked at the lesson until now, for please let me revise that in ES5 terms…

// callee

var foo = function (bar) {
    return bar * 2;
};
// caller

console.log(foo(21))    // -> 42
foo(21)

The above call passes the argument (21) to the foo function. The formal parameter of the function is bar, an arbitrary variable name that is only accessible locally and which accepts the argument passed to the function.

The return statement sends the computed value back to the caller where it can be outputed or further processed.

In your lesson you are using a function call as the argument in an if statement.

if (quarter(12) % 3 == 0)

The conditional expression waits for the return value from the quarter function which it then applies the modulo operation to and compares the outcome to 0. If true, then we know that the return value is divisible by 3.


#3

Oh, this helps a lot. Thank you!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.