Return a Value from a Function with Return

return key word is a new concept for me

not sure what’s going on

i know what “return” means in the English language…

what does “return” mean in JavaScript?

what does it do for me?

simply, how does a return work?

start at the very basics, please : )

some code

function plus2(num) {

return num + 2;

}

console.log (plus2 (10));

please walk me through this code regarding the return… explain what’s going on

please pass on any other advice that you feel would be helpful

maybe pass on other return code examples?

MANY THANKS!!

the same, handing back/returning something.

for example if we had:

function example() {
   let x = 3;
}

example();
console.log(x);

we would get an error, given x is a local variable, it only exists within the body of the function.

this is where return comes in:

function example() {
   let x = 3;
   return x;
}

let x = example();
console.log(x);

First, i want to thank you for your time and guidance!!!

OK! making progress!

i found the simplest return code i could

<script>

function addingNum (a, b) {

var c = a + b;

return c;

}

console.log (addingNum(2, 5)) ;

now (addingNum(2, 5)) at the bottom fills in the addingNum (a, b) in the function up top yes?

the var c now shows 7…

HERE WE GO!!

return c is giving me back ,returning, what i put in a + b

am i right? :smile:

if not, where is my error?

still having trouble understanding your example… this is some tough code and i am just starting the return concept

i will continue to study it and figure it out or, simplify it? baby steps?

again, thank you!!

yes, a and b are parameters, they get there values from the arguments (2 and 5) at function call.

a + b is addition, the result of which you store in c variable.

you then return c, so now the function call gets this value returned, which we can log:

console.log (addingNum(2, 5)) ;

or store in a variable:

var d = addingNum(2, 5);

so my c variable is my return?

whats the difference between return c and console.log (addingNum(2, 5)) ; ?

i may be missing something…

or you can return the sum directly, the intermediate c variable isn’t doing much:

function addingNum (a, b) {
    return a + b;
}

console.log (addingNum(2, 5)) ;

addingNum(2, 5) is a function call, which receives/gets the returned value, which you can capture in a variable or log like you did

thank you so much for your patience! seriously

a + b is my return? i dont get where my c variable comes in

maybe we should back up a bit :slight_smile:

i understand the word “return”. you kindly explained that to me above

maybe my first confusion is what code is being returned and why… obviously that is a difficult concept for me

maybe give me more examples of returns? dumb it down a bit for me?

what makes something returnable?

again, many thanks! so far you have been polite, patient, friendly and taking your time to help a confused coder

BOY if this is an easy concept and i simply do not grasp it…

i guess some concepts come easier or harder depending on the coder

you could store the result of the sum (a + b) in a variable, and then return the sum/c variable. But you can also return the sum directly like I did

return doesn’t have a different meaning in programming, its returning/handing-back a data to the function call:

function addingNum (a, b) {
    return a + b;
}

// storing returned value in d variable
const d = addingNum(2, 5);

the function call (last line), receives the returned value, which we then store in a variable (named d)

nothing in particular, we can return whatever we want. Like I demonstrated earlier we can’t access data which is inside the function outside the function:

function example() {
   let x = 3;
}

example();
console.log(x); // error

this is where return comes in.

many thanks!

i will review our chats and try to find my answers

if not, i will continue my search and come back to you if need be : )

again, thanks!

Ask yourself, given we are operating in a different scope, how do we get the data we create back to the calling scope? We return it. Well written code will be set up to receive it.

It makes little sense to compute the volume of a granary if we do not know the area of its base. Here we set that up…

const area = radius => Math.PI * radius ** 2;

Given a radius, the above function returned the area of a circle with that radius. That is the area of our base. Now we compute the volume of our granary given a height.

const volume = (radius, height) => area(radius) * height;

The area function is returning a value that is being then used in the multiplication that produces a final result. Lo and behold, our function returns that result to us.

console.log(volume)

Let’s consider the granary. We are told that a 22 foot by 33 foot granary will hold 10000 bushels. Do we believe that?

Our code smarts kick into action…

const area = radius => Math.PI * radius ** 2;
const volume = (radius, height) => area(radius) * height;
const bushels = volume(11, 33) / 1.24446
console.log(bushels)

What do we get as a result?

10080.178925625609

So the information we were given was correct, with a margin, to boot.