Function returns.... need some help

Hi

i understand how to pass arguments to a function, but having trouble getting content from a return

thats pretty much all i know about returns

please explain whatever you think i should know about returns, either here or an another resource

also, “message” does nothing for me in my text editor

some code i have been working on

<script>

function bake(degrees) {

    var message;

    if (degrees > 500) {

        message = "I'm not a nuclear reactor!";

    } else if (degrees < 100) {

          message = "I'm not a refrigerator!";

    } else {

          message = "That's a very comfortable temperature for me.";

         // setMode("bake");

       // setTemp(degrees);

    }

    return  message;

}

var status = bake(350);

// Here so the code will run!

  </script>

the first line i get… but why is my argument for the function name parameter at the bottom?

what needs to happen to get back a return?

also, please explain what the var status = bake(350); is doing… though i want to say this… “Now, when the function is called and returns, the string that is returned as a result will be assigned to the status variable” …

taking bake(350); away?

please ignore the commented out code… not there yet!

i kindly ask for some clarification on the above, please explain as simply as you can! JS is still a rough up hill battle for me!

maybe provide a few examples of returns in work

if i am missing something, let me now :slight_smile:

MANY THANKS!

Your function defines what parameters it will take, and then the function call or invocation gives a value to put into that parameter.

Think of the function bake as reading "Here is a blueprint for bake".
Then bake(350) is “take the bake blueprint, and input 350.”

Like a car factory, might define a function car(color). This blueprint can then take any color, and the instructions are the same (make car, paint it whatever value is in color). So the system then knows how to make cars. Later on, the factory decides it wants to make 2 blue cars and 1 red car.

car('blue');
car('blue');
car('red');

Your understanding is correct.

x = 3+3
think about how you do this. In order to figure out what value to give X, you have to evaluate the right hand side of the assignment operator (the =).
3+3 = 6, so we assign the value 6 to X.

var status = bake(350);
same thing. We need to evaluate the right side of the assignment; so we run the function, and whatever value the function returns (all functions return; if the function does not specifically output a value, it returns undefined) goes into the variable.

is this from my var status = bake(350)? at the very bottom? bake(350) is the argument being passed to the parameter up top?

i am taking the bake(350) from the right side of the operator

does that mean my var status = bake(350);will be replaced with the var = status proper string?

i hope i am not confusing!

BTW, what does “message” do for me?

also, what need i do in my text editor to get a return with the above code?

please know i sincerely appreciate your time and guidance… look at it like this… you are helping me become a better JavaScripter :grinning:

As my JS is basic maybe my basic explanation may help :slight_smile: (or not)

You are calling the function bake() and passing 350 as a parameter. i.e. bake(350).

Then when the function runs the degrees (argument) will equal the 350 you sent it. That will then set the value of message to “That’s a very comfortable temperature for me.” (because degrees are greater than 100).

You are assigning the value of bake(350) to a variable called status. The variable status therefore contains the value of the message variable that you returned. If you were to write out the value of status you would see the value of your message.

e.g.

var status = bake(350);
console.log(status);
// will show "That's a very comfortable temperature for me."
status = bake(50);
console.log(status);
// Will show "I'm not a refrigerator!"

status = bake(600);
console.log(status);
// Will show  "I'm not a nuclear reactor!""

If you run that with your code you will get the message displayed three times as shown above.

From the above you can see that you can call the function bake with various values and receive the appropriate answer in the variable called status.

to make sure we’re not confusing things here just a little bit; on top of what Paul mentions, var is just javascript’s declaration “This is a variable. Its value may change at some point.” (As opposed to const, which means a value is constant.) So var status = bake(350) reads as:
“Create a new variable, called status. Assign to that variable the value returned from the function bake, when passed the value 350 as its first argument.”

1 Like

ok, i am close :slight_smile:


<script>

        function bake(degrees) {

            var message;

        

            if (degrees > 500) {

                alert ("I'm not a nuclear reactor!");

            } else if (degrees < 100) {

                alert ("I'm not a refrigerator!");

            } else {

                alert ("That's a very comfortable temperature for me.");

                setMode("bake");

                setTemp(degrees);

            }

            return alert;

        }

          

        var status = bake(350);

        

        // Here so the code will run!

        function setMode(mode) { };

        function setTemp(degrees) { };

        

          </script>

i changed “message” to “alert” and it is flawless

i get its

 

`var status = That's a very comfortable temperature for me.`

but also isn't `return  alert;` the same string?