Can someone please explain how parameters not defined in codes are just called out of nowhere?

In this part of the code: const getTea = (prepareTea, numOfCups) => {,
prepareTea is not defined or linked to the variable prepareGreenTea or prepareBlackTea. So does this mean one can just put in any random variable in a function without assigning or initialising it? I’m confused. Please don’t tell me it takes prepareGreenTea or prepareBlackTea as an argument (I already know that). Question is why is an undefined variable (prepareTea) been used as a placeholder without it being defined or explicity linked to the variables it is placeholding for? By the way, same thing goes for numOfCups. Thanks in advance.

  **Your code so far**
// 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();
return teaCups;

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

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Understand Functional Programming Terminology

Link to the challenge:

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

This is a function definition which takes two arguments named prepareTea and numOfCups.
And this is where the function is called:

const teaGreenTeamFCC = getTea(prepareGreenTea, 27);
const teaBlackTeamFCC = getTea(prepareBlackTea, 13);

Apparently prepareGreenTea and its black counterpart are also function defined on top of the code. When called, they return the strings greenTea and blackTea respectively. They are called within the getTea function, under the alias prepareTea
I know, it looks quite confusing and references are all over the place, but this is how more extensive JS code looks. There is lot of passing of objects/functions and assigning to variable values. You need to watch carefully to follow what comes from where. It is important to understand how functions work, how their parameters work, what it means when they are called and when they are simply references, or when they are declared.

// declaration
const myFunc = () => console.log('do something')

// execute the function, its code is activated

// simply reference the function, assigning it under another variable
const newFunc = myFunc

// does whatever myFunc does

// or i can assign the value it returns to a variable(currently the function returns undefined)
const funcValue = myFunc()

// we can also pass it to another function as a callback
// here for a difference i decided to declare function with the function keyword
// but that can be done with const or let too, altho there are some differences
function takeCallback(callbackOne, callbackTwo) {

takeCallback(myFunc, newFunc)

It can be fun to juggle with functions and can allow you do some amazing things once you get the hang of it

I appreciate the explanation. However, the const getTea = (prepareTea, numOfCups) => { … just explains what I already knew - it takes two arguments called prepareTea, numOfCups, yes. But I still have no idea where these were defined. Where is the var or const bit which you need to define them. Except I’ve been learning the wrong thing this entire time, don’t you need to define variables before you begin to use them?

I’m not sure if understand the question.

You do not declarer parameters using keywords (var, let, const) you never have. Not sure why you at this point in the curriculum would think otherwise (unless I’m misinterpreting your question).

Parameters are part of the function definition. The value the parameter contains is whatever was passed to the function when called.

prepareTea and numOfCups are parameters part of the getTea function definition. They are passed a function and a number as arguments at the call site.

Edit: MDN links if needed.

function add(a, b) {
  return a + b;

console.log(add(4, 2));

In this simple code it’s the same way. You can name function parameters whatever you want.

1 Like

Figuratively, this is where they are defined/assigned. On the function call, the two parameters are being initialized with the passed values. It would look something like this:

prepareTea = prepareGreenTea
numOfCups = 27

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