Homework- cant wrap my head around


#1

Which of following code properly calculates the cost of 5 watermelons, which cost $8 each?
A. function calculate(cost, 5, *){
return cost * 5;
}
calculate(cost, 5);
B. function calculate(cost, 5){
return cost * 5;
}
calculate(5 * 8);
C. function calculate(cost){
return cost * 5;
}
calculate(cost);
D. function calculate(cost){
return cost * 5;
}
calculate(8);

the answer is either and i just don't know. Can someone walk me through it ?
C
A & D
A, B & D
D
B & D


#2

The answer is (D). Is this an actual question or a quiz?


#3

it is not a quiz its homework for a trade school i am going to idk why I cant wrap my head around it


#4

Ahah, Sorry to ruin that for you. But it's dependent on the function.

Functions work like so:

function calculate(cost){
  return cost * 5;
}
calculate(8);

Basically, the function just has it's parameter, so no matter what the function knows what belongs in it.

in this case the parameter is "cost". This parameter will only work in the function and nowhere else.

so as soon as you call the function and pass it a new parameter, the function will then change all of that parameter, to the variable or value you've given it.

calculate(8); //the computer sees this

function calculate( 8 ) { /*then it comes back to this line of code and 
                            replaces the parameter*/
   return 8 * 5; //return the parameter * constant
}

The return statement is just returning the value, "40".


#5

Man... why can't you just be my teacher -.- again thanks for taking the time to explain what is going on and not just giving me the answer :smiley: