How does a function work?


#1

This is my code some how it does not work?

// Write your foodDemand function below.
// Last hint: In your reusable block of code, end each line
// with a semicolon (:wink:
var foodDemand = function(food); {
console.log("I want to eat" + " " + food);
}
foodDemand = "Watermellloooon"

this is the error

Oops, try again. There was a problem with your syntax.


#2

Hi you should call your function like that

foodDemand("here type your food name")

#3

But wouldn't you be printing it out????!?!?


#4

Consider this line as a single statement:

console.log('I want to eat watermelon');

It can only run once, and cannot be changed. However, if we wrap it in a function wrapper and allow it to accept inputs, we can re-use the core text with variation.

var foodDemand = function (food) {
    console.log("I want to eat" + " " + food);
};

To execute the code, we use a call expression,

foodDemand("Watermelon");

In the above, 'Watermelon' is called the argument, which in the function is a formal parameter given by the variable, food. Notice that food takes on the value, Watermelon?


#5

Oh ok I get it now thanks for helping me