Functional Programming- Cup of Tea-use of ()

Hi all -
I have a quick question regarding the prepareTea function. I see it as an argument for the variable getTea.

const getTea = (prepareTea, numOfCups) => {

We later use prepareTea for the variable teaCup, but this time we use parenthesis. Why is that?

  const teaCup = prepareTea();

I know we use parenthesis for functions, but I guess my questions is why don’t include the parenthesis when passing it as an argument the first time…like this:

const getTea = (prepareTea(), numOfCups) => {

As always, thanks for your feedback!


// Function that returns a string representing a cup of green tea
const prepareGreenTea = () => 'greenTea';

// Function that returns a string representing a cup of black tea
const prepareBlackTea = () => 'blackTea';

/*
Given a function (representing the tea type) and number of cups needed, the
following function returns an array of strings (each representing a cup of
a specific type of tea).
*/
const getTea = (prepareTea, numOfCups) => {
const teaCups = [];

for(let cups = 1; cups <= numOfCups; cups += 1) {
  const teaCup = prepareTea();
  teaCups.push(teaCup);
}
return teaCups;
};

// Only change code below this line
const tea4GreenTeamFCC = getTea(prepareGreenTea, 27);
const tea4BlackTeamFCC = getTea(prepareBlackTea, 13);
// Only change code above this line

console.log(
tea4GreenTeamFCC,
tea4BlackTeamFCC
);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Understand Functional Programming Terminology

Link to the challenge:

Hello :slight_smile:!

The first code:

const getTea = (prepareTea, numOfCups) => {

Is defining a function that receives a reference to another function and a number (the cups of teas to prepare).

The second code you posted is calling a function:

const teaCup = prepareTea();

If you pass the name of a previously defined function (or one that’s in the current context) as an argument to another function, that argument is considered a reference to a function, which then you can use/call/invoke.

However, if you were to add parentheses immediately after a function name, then you’re calling/using/invoking it instead, which would actually run the code inside the function’s body:

// Here we're defining a function,
// but it hasn't been invoked/called yet.
function myFunc() {
  return 10;
}

// Here we stored the result of calling/invoking/using myFunc
// In other words, the code inside myFunc has been executed
const storedResult = myFunc();

console.log("Stored result: %d", storedResult);
// Output: 10

// Here we would not be calling/using/invoking the function.
// Instead we assign a reference to the function
const referencedFun = myFunc;

// And now we call/use/invoke the function
const whatShouldBeTheResult = referencedFun();

console.log("What should be the result? %d", whatShouldBeTheResult);
// Output: 10
// Yep, it prints 10 too

Hope it helps :slight_smile:.

1 Like

The parentheses call the function (when you are not using them to declare the function) ,you don’t want to call the function when passed as an argument.

prepareTea is a variable to use to call which ever function you pass in, in this way you can get black or green tea.

2 Likes

Thank you for this. I appreciate the help!

1 Like

It certainly does. Thank you so very much. So helpful!!! I appreciate it!!

2 Likes

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