Return function


#1

Hello everyone,

I recently started with trying to understand programming.
That's why i'm here. I'm not a native English speaker so
there could be some mistake in my grammar or vocabulaire :wink:

I don't know how a specific part of this function works. Especially
the part in which they insert 3 in the getSubTotal(itemCount) function.
I have found out that it has to do with the takeOrder I inserted, but
how does the takeOrder(s) make up 3 in the itemcount and how does
that result into the 22.50?

`

var orderCount = 0;

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));

/output is 22.50
`


#2

This is a global variable used to keep track of how many times the takeOrder function is called. Notice that inside that function we add 1 to orderCount. The statement accesses the global variable to be able to do this.

In the above, itemCount is a locally defined parameter, but if we look at the call,

we see that the global variable is passed in. Inside the function, itemCount will be that value.


#3

Thanks,

I think I'm getting the hang of it now.
So for example if I put an extra takeOrder function there will be added 1 into the orderCount function
because that is the global variable for the amount of takeOrder functions. But if I for example change the
orderCount function to orderCount = orderCount + 2, will there be added 2 for every takeOrder function instead of 1?


#4

Yes, that is how it works, but adding 1 makes sense. Adding more than one would need some more justification, but you're getting the idea.


#5

Hi! I have some questions regarding the same topic.

In the code, I don't understand why we have to use itemCount (line 8), and what does it do in the code?

Instead of using itemCount, I used orderCount in the parameters of getSubTotal, and the results are the same (22.50). Can you please explain why this is the same?

Lastly, what does return actually do? I don't quite understand the terminology given by the website.

Thanks in advance! And i hope my questions are understandable :smiley:

var orderCount = 0;

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

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

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

console.log(getSubTotal(orderCount));


#6

Functions are reusable blocks of code that can take in data, operate upon that data and send a result back. The function is given the nickname, callee to which we may pass arguments (input data) that are taken into the function through its parameters. The object passing the arguments is given the nickname, caller.

caller -> (arguments) -> callee (parameters) -> [process] -> return values

The caller is in one scope, the callee might reside in that same scope, but the code block it houses does not. It has its own scope, called local scope.

Data in local scope cannot be seen by the caller, only the callee. return is the means by which data can bridge the two scopes allowing the exchange of parameter data for resulting process data.

 caller scope  => callee scope => caller scope

When we use the same variable inside our function as the one in caller scope, we no longer need a parameter. Our function can see and access global variables.

function getSubTotal() {
    return orderCount * 7.5;
}

will do exactly the same thing as your code. When we define a local variable in the parameter, it shadows the global variable, and takes only its value into the function. The function processes that value and returns a result to the caller. This is the case only if the argument is a value or expression (expressions yield values). When the argument is an object or array, it is a reference object that is not shadowed.


#7

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