Question about parameters


#1


https://www.codecademy.com/courses/learn-javascript/lessons/functions/exercises/return?action=lesson_resume


In the code below, I don't understand the "getSubTotal" function. "itemCount" inside the function doesn't have a specific value. Such as "itemCount = 1;". So how is it possible for "console.log(getSubTotal(orderCount));" to result 30 (on the console)?


Replace this line with your code. 
var orderCount = 1;

function takeOrder(topping, crustType) {
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
  orderCount = orderCount + 1;
}

function getSubTotal(itemCount) {
  return itemCount * 7.5;
}

takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log(getSubTotal(orderCount));


Functions: return pizza.js incorrect answer
itemCount value?
#2

itemCount is a paremeter(local variable) , do not get confused with global variable orderCount,.

orderCount keeps tracks of order and we pass it as argument of getSubTotal() function, The parameter itemCount is replaced by the value of orderCount.

See how parameter is replaced by value when we pass values to function.


#3

I'm getting slightly confused...
By replaceable, do you mean the value of orderCount is equal to the itemCount parameter?


#4

If you have watched the mini video ? You'd have noticed that parameters are nothing but hollow variables(no explicit visible value), a placeholders, when we pass argument,they are simply replaced by the values.


#5

Parameters are not even placeholders. The arguments list is well enough equipped to take any number of arguments. Parameters are order keepers. They represent the order in which the argumnents are assigned to local variables. Just because it is not listed as a parameter does not mean it is not present as an argument. Most languages are open-ended this way, just as Python allows for default parameters for which there are no arguments.


#6

To test this, create a function with no parameters, say,

var foo = function () {

};

Inside the function we will query the arguments object which is a collection and is iterable.

    for (var i = 0; i < arguments.length; i++) {

    }

One can guess what the next line will be,

        console.log(arguments[i]);

Now let's call the function with a huge list of arguments...

foo('a', 1, 'b', 2, 'c', 3, 'd', 4, 'e', 5, 'f', 6);

Output

a
1
b
2
c
3
d
4
e
5
f
6

#7

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