# Functions lesson 6.Return II

Hi all,
Can someone help explain what is going on in this code?

``````let orderCount = 0;

const takeOrder = (topping, crustType) => {
orderCount++;
console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};

takeOrder('mushroom', 'thin crust');
takeOrder('spinach', 'whole wheat');
takeOrder('pepperoni', 'brooklyn style');

const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};

const getTax = () => {
return getSubTotal(orderCount) * 0.06;
};

const getTotal = () => {
return getSubTotal(orderCount) + getTax();
};

console.log(getSubTotal(orderCount));
console.log(getTotal());
``````

I get what is happening up to the

``````const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};

const getTax = () => {
return getSubTotal(orderCount) * 0.06;
};

const getTotal = () => {
return getSubTotal(orderCount) + getTax();
};

console.log(getSubTotal(orderCount));
console.log(getTotal());
``````

portion.
For

``````const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};
``````
1. What is itemCount used for? It is not used anywhere else in the code so what does it contain? I understand that the function is calculating a subtotal but what are the values contained in itemCount its self? I tried to print out its value but the program just errors out.

2. What is held in getSubTotal(orderCount)? It never had a value passed to it, getSubTotal had itemCount*7.5 passed it.

`itemCount` is a parameter, so it gets its value from argument at function call:

``````// function decleration with parameter itemCount
const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};

// function call with argument orderCount
getSubTotal(orderCount)
``````

so `orderCount` (which contains the number of orders) is passed to the function parameter (`itemCount`) when the function is called.

function parameters are variables, but they have a local scope, the only exist within the function.

the confusing thing in your program is, that getSubTotal function call is within another function (getTax and getTotal)

which is also the reason we have the getSubTotal function, in programming, DRY (donâ€™t repeat yourself) is a very important principle), and give we need to use getSubTotal twice, we made a function out of it.

Yes, we could calculate getTotal in a single go, but doing it the way you do, makes our program more modular. Which is good practice, although it might make things a bit confusing at first

1 Like

Thanks @steim94,
Sorry, maybe itâ€™s how I worded the question. Can you point out what line in the code we pass orderCount to itemCount? That is what I am stuck on right now.

The code provided is what is given when I selected the â€śGet Codeâ€ť option. I got stuck on the instructions
â€śInside the getTax() functionâ€™s block, call your getSubTotal() function to get the subtotal and then multiply the returned value by 6% (0.06). Make sure to return the result of this operation.â€ť
I originally took these instructions as below since I thought that now contained the subtotal amount. But that gave an error.

``````const getTax = () => {
return getSubTotal * 0.06;
};
``````

I think I get what the â€śgetSubTotal = (itemCount)â€ť portion is doing now, thank you for the explanation. It is passing itemCount*7.5 to subTotal, correct?

That is the line that passes the global as an argument. The parameter is local identifier of that value.

1 Like

Does this mean that orderCount is considered itemCount now?

Itâ€™s value is represented by `itemCount`, but only inside the function. Nothing happens to the global.

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