How to use "return"

I get the gist of “return” but I don’t feel super comfortable with it. I’m memorizing how it works instead of learning how it works, can someone help explain it.

The return statement is used to send a value back from a function. Often times a function will except input, do something with it, and then send a value back based on that input. The return is what allows it to send a value back. For example:

//          add is a function
//          /   n1 and n2 are parameters that will hold input sent to
//        /    / \        to the function
function add(n1, n2) {

    let new_number = n1 + n2;
//         \
//      new_number is created withing the function based on the input
//      given to it

    return new_number
//     \
//     finally the return sends the value of new_number back to were
//     the function was called
}

/*    the value of this variable will equal to the value returned from add()
      /          the return will send back a value based on the function
     /            / \         parameters                                                                      */
let number = add(3, 5)
/* 
   since 3 and 5 were supplied for n1 and n2 in add(), the function 
   will return a value based on them, or 8.
*/

This is of course just one example, a return can send back any value, with the exception of variables outside the function scope. (if you haven’t learned about scope yet, don’t worry you will get there soon.)

1 Like

Thanks so much. This really helped me understand it.

1 Like

Hi there,

May i ask why is there a need to create let new_number = n1 + n2; inside the function?

And when calling why has the variable name changed from new_number to number?
let number = add(3, 5)

This is still very confusing. It will be great if it can be more layman terms and using arrow to point. Thanks in advance! Appreciate

1 Like

The function I wrote out was actually overly complicated (in fact, since you can just say 1 + 1, there is no reason to write the function), there is no actual reason to create a variable just to return it. I did that just as a demonstration that you may have to perform actions within the function in order to obtain the value you want to return.

These are actually different variables. new_number is an example of a variable created inside the function to be used as storage for whatever is needed. Variables created inside of a function are deleted after the function finishes running.
The variable number is created to store the result of using the function add().

Something to note is that variables can be named nearly anything, and their name does not effect their behavior. Their name is chosen by you, and usually (but not always) indicates what they store.

An easy way to look at a function, is to compare it with an instruction manual. For the add() function the steps might look like:

  1. Get two numbers.
  2. Add these two numbers together.
  3. Give the result of step 2 back to whoever was asking.

In code it looks like:

//              get two numbers
//             /
function add(n1, n2) {
  let n = n1 + n2;   // Add the two numbers together
                     // Note that the variable n is used purely as 
                     //  temporary storage, it is deleted when the
                     //  function ends

  return n;          // Give the result back to whoever asked
}

When you call the function, it is similar to following the instructions listed for whatever values you are using.



For clarity I would like to point out that the function could actually be written more simply as:

function add(n1, n2) {
  return n1 + n2;
}

There is no actual need to create a variable since the operations are simple enough to be on a single line.

Thank you so much for the explaination ! Appreciate!

I tried to read today and slowly digest word for word in case i miss out any key point.
I think it makes more sense now!

Still trying to comprehend everything to apply them to writing the codes. Brain fried today. I guess it takes time!

Thank you once again!