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.

Hello again

i have been trying to solve the return concept… quite the headscratcher for me!

in codecademy lesson Return i found


function monitorCount(rows, columns) {

  return rows * columns;

}

const numOfMonitors = monitorCount(5, 4);

console.log(numOfMonitors);

ok…

then i get help on that specific topic and it goes

" This lesson is so wrong. lol. I believe the tasks are backwards. Why do I have to use the return keyword before declaring the variables, in this case numOfMonitors ? The examples wasn’t like that.

next

The exercise to this Return lesson should be like this and not otherwise:


function monitorCount(rows, columns) {
const numOfMonitors = rows * columns;
return numOfMonitors;
}

console.log(monitorCount(5,4))

so i am confused here

a little earlier i found old (years) posts on returns… that where i will go next

i kindly ask you to clarify…

function ademphasized textdingNum (a, b) {
    return a + b;


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

return a + b
return a + b; comes from my parameters? or the arguments being passed through the parameters?

or does the return tell me how i am using parameters? in this case addition

or is it what i am doing with addingNum(2, 5); ?

i take addingNum(2, 5 pass it into the addingNum(a, b)

and the return is a + b because that what happened with (a, b)

again, thank you!

because here:

function monitorCount(rows, columns) {
const numOfMonitors = rows * columns;
return numOfMonitors;
}

the intermediate variable (numOfMonitors) isn’t really doing anything. We can just get rid of it if we want

piece of code with comments where I specify the terms:

// function declaration with 2 parameters
// parameters are: rows and columns
function monitorCount(rows, columns) {

  return rows * columns;

}

// function call with arguments
// argument values are 5 and 4
// store returned value in variable
const numOfMonitors = monitorCount(5, 4);

console.log(numOfMonitors);

the arguments are passed along to the parameters. The parameters act as placeholders until they (The parameters) get there values from arguments at function call.

under the hood, JavaScript will assign the arguments to the parameters by position

why is my return a + b and not 7?

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

ok, confused here…

addingNum(2, 5) is NOT my return

these may be the concepts i need to understand returns

lets try another example, simple

  function square(n) {
       return n * n;
    }

now return n * n is being returned because its a calculation?

i learned that a variable or expression can be returned

let text = mytext
return mytext

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

also,

function get_complete_text () {
let text1 = "This is ";
let text2 = "confusing";
let completetext =  text1 + text2;
return completetext;

}
ok… what is being returned (beside it being a variable?)

the return is last

now instead of strings, how does it work with arguments? yes, i understand arguments and parameters

i dont want to annoy you with this and i dont want you to feel you are repeating yourself.

you have helped me and never gave up… which is cool and i appreciate it!

i am determined to learn this concept!!!

because functions allow us to eliminate repetitive code, so if we add another function call with different argument values:

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

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

we expect 11 for the second function call.

return is return, the return keyword is what the function hands back to the function call

the purpose of the function is to calculate the square, so now that task is done, we need to hand back/return the value so we can use it outside the function

n * n is an expression

variables are essentially just labels, the string is actually returned.

same as the functions you have thus far:

function get_complete_text (text1, text2) {
   let completetext =  text1 + text2;
   return completetext;
}

:slight_smile: :slight_smile: :slight_smile:

its starting to click

BINGO!!

I GOT IT!!!

makes sense now :smiley: :smiley: :smiley:

want to know what my problem was??

i didnt know i could return whatever i wanted after the return keyword!!!

am i right?

you had mentioned that too i think!

WHAT a beast of a problem for me!!

ok, 2 questions for you…

how does this help undefined code, you had mentioned something like that prior

maybe explain from the beginning what undefined means in this circumstance…

and when will i be using the return keyword in my projects? under what circumstances? where does it fit?

THANK YOU!!!

how much of my first million do i owe you? :smiley:

Did a search for: undefined, no matches found. Maybe you can quote what I mentioned earlier?

Very likely, return is used often. Its important to understand what return does and how it works. This is the first step in understand where it fits

from above

what does scope have to do with this? global vs local?

our “error” is an undefined, yes?

what does “undefined” mean? in a simple context used here

what does the return do for me?

how does the return change things?

again, i sincerely appreciate your time and guidance!!

You’ve been waiting for five hours so I’m just going to jump in with some responses to your questions. @stetim94 may have other comments to add.

let is scope defining. Access to the variable is confined to the block in which it is declared.

../index.js:27
console.log(x);
            ^

ReferenceError: x is not defined

It means there is no definition in the form of a declaration and assignment, or in function terms, it is the absence of a return value. JS wants to return something, always, so when nothing is given, it returns the only thing it can, undefined.

The invoking statement is the caller. return passes its value back to the caller, where it is assigned to x in the example above.

It makes the function usable in that it always returns a defined value that can be used elsewhere. Note, return by itself, with no value will be undefined, same as if the statement is left out.

thanks!

please elaborate