Help understanding Return functions


#1

I am working on the functions section of the course, and am rather lost in how the return action interacts with the functions. Specifically, step 6 of 10 is what is confusing me.

I failed enough times that I was given the answer, and when I read it, I don’t fully understand why some of the code is included. The code is

1. let orderCount = 0;

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

6. takeOrder('mushroom', 'thin crust');
7. takeOrder('spinach', 'whole wheat');
8. takeOrder('pepperoni', 'brooklyn style');

9. const getSubTotal = (itemCount) => {
10.   return itemCount * 7.5;
11. };

12. const getTax = () => {
13.  return getSubTotal(orderCount) * 0.06; 
14. };

15. const getTotal = () => {
16.   return getSubTotal(orderCount) + getTax(); 
17. }; 

18. console.log(getSubTotal(orderCount));
19. console.log(getTotal());

Why do lines 13 and 16 have getSubTotal(orderCount) but line 9 sets the constant getSubTotal to ‘itemCount *7.5’? Shouldn’t lines 13 and 16 just need to be ‘return getSubTotal()’ and then their extra parts? If not, then why is line 9 and 10 there and it seems we’re working the same problem again in lines 13 and 16? itemCount seems to be made up simply for line 9 and never referenced again.

I thin I’m over thinking this and spiraling myself into more confusion.


#2

@cyborgtriceratops the syntax of a function is:

const  functionName = (Argument1, Argument2, ... ) => {
       function 

So in line 9 itemCount is the argument for the function, and in line 13 and 16 within the parentheses of getSubTotal you are mentioning what the argument will be.


#3

I’m a beginner also, but what i have learned in that section is that functions are meant to split calculations or data into smaller parts and also for re-usability in the program you are creating.

In this case we have 3 different functions which is:

line 9: getSubTotal, which takes in how many orders there are and calculates the subtotal.

line 13: getTax, calculates the tax by reusing the function getSubTotal.

and lint 16: getTotal, reuses getSubTotal and getTax function.

But of course you are right you can definitely combine all of them together.


#4

Rereading it, and reading your answers made it much clearer to me. Also a good night’s sleep seemed to have helped. Thank you!


#5

I chatted with Jimmy and asked this question “why do I need an ItemCount variable when itemCount is the same as orderCount?”

He said:

  1. “itemCount is the argument for that function and takes the value from orderCount and uses it just for the inside of that function, that’s so you pass in the information needed and if you make any changes to itemCount, it won’t effect orderCount:”

  2. "generally using global variables is bad practice "

  3. "it comes into the factor of in real like when you don’t know the exact order of a program, your not going to know when and where the variable changes "

  4. so that’s the point of the parameters, they exist only inside the function and that’s it so itemCount only exists inside getSubTotal.

I hope this helps but I am still somewhat confused.


#6

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