Can a function use variables, even if those variables are not passed as parameter?


#1

In getTax() and getTotal() function we are calling getSubTotal() function which uses orderCount variable/parameter/argument but orderCount is not passed to getTax or getTotal unlike the example quoted here where celsius is passed in getFahrenheit(celsius) even though this function doesn’t directly use celsius.


#2

If you are calling a function inside another function it doesn’t mean second function use the variables of first function.
parameter is just a placeholder.you can take any name (of course there are some rules) here.

from the lesson example

function multiplyByNineFifths(celsius) {
  return celsius * (9/5);
}

function getFahrenheit(celsius) {
  return multiplyByNineFifths(celsius) + 32;
}

every function has its own scope. so here though both function take same name as parameter but both can have different values .don’t just confuse about names.For getFahrenheit function you can take another name as parameter.for example

function getFahrenheit(c) {
  return multiplyByNineFifths(c) + 32;
}

it’ll work same. so when we pass an argument for getFahrenheit function c just holds that value.then same value passed as argument for multiplyByNineFifths function. so getFahrenheit doesn’t know about celsius.

Hope it helps :slight_smile:


#3

It doesn’t need to be passed since it is global and both functions can access it.


#4

I had the same question, Thanks!


#5

Thank you!! Your response helps.


#6

Thanks you!!

Your response helps.


#7

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