Beginners "return" concept

this was suggested for functions

please see 15:51 - 16:29

i am lost here… big time

i have watched it multiple times, no go for me :frowning:

i understand “return” means to give back, yes?

what am i giving back… what am i “returning”?

how does a return work? why?

how did i arrive at a return to start with?

what does return a variable do?

please go slow and simple :slight_smile:

i am truly lost on the “return” concept as a whole

and, please, whatever else that would be helpful

thanks!

The following contains a simple and concise definition with examples - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

See if reading the information, which you can convert to your native language if it is not English, is more helpful then watching a video.

1 Like

thanks

will take a look!

ok, progress!

off the top:

i know all of this is simple, but its where i am coming from

what is " function execution"?

what does “The return statement ends function execution” mean?

does the return statement always start with return?

what does “returned to the function caller mean”?

what is my function caller?

please know i appreciate your time, assistance and patience :slight_smile:

You need to think of a functions as being their own little programs or sub-programs.

Let’s take calcArea as an example.

When you call the function calcArea(5) the main code stops what it’s doing and goes off and runs the calcArea function. When it has finished running it returns back to the main code and the code continues.

By default a function returns whether it has a return statement or not. If there is no return statement it returns a value of undefined.

const foo = function() {
  // just a comment here
}

const bar = foo()
console.log(bar) // undefined

The return statement ends function execution

Basically it does what is says on the tin. If we come to a return statement in the functions code, it will finish there and return to the code in which it was called.

Does the return statement always start with return ?

Yes, but as mentioned above a function will return by default.

What is my function caller?

I would think we are referring to where the function was called from — the environment or context. So in the diagrams I have given you, the Main code would be the function caller for calcCircleArea.

Lastly

Here is a diagram to illustrate explicitly returning a value

I hope this helps :slight_smile:

1 Like

again, thanks!

lets back up…

this is a function i found

const speak = function(name, time){
console.log(`good ${time} ${name}`); 
}
speak ("evening", "Matt");

this i do get!

is there a return there?

on to returns…

your first example was a bit confusing

i looked for the simplest return i could… baby steps, right?

how about

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

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

how does return work here?

what is console.log (addingNum(2, 5)) ; doing for me?

can you se where i am coming from?

lets take it slow and simple :slight_smile:

Yes, but not set explicitly as it would be with a return statement

const returnValue = speak ('evening', 'Matt') // will log out 'good evening Matt' and return undefined.

console.log(returnValue) // undefined

Returning explicitly

addingNum is called first passing in 2 and 5. When addingNum returns 7 it is passed to the console.log function, which then outputs it. Essentially

console.log(7)

I did take the time to go into some detail with my earlier response. If you haven’t done so please do have a look at the diagrams I made.

In addition try experimenting with your own code.

Furthermore I would highly recommend reading about functions from numerous sources. They may well help you build the pieces of the puzzle.

To give you an example. Recently I was trying to wrap my head around a functional programming concept called transducers.

There is quite a bit to keep track of, and I was struggling. I read my go to book Mastering Javascript Functional Programming, I read Functional programming in Javascript, I read from various sources online and finally a recently bought copy of Composing Software by Eric Elliot. On the last book, Bingo!, it finally fell in to place. On returning to the other books, the explanations made perfect sense.

In short it takes a bit of time and effort, but if you put in that effort you will get there:)

1 Like

again, thanks for your patience

this is my first step… this is starting to click here… as a return and a call

i will study your examples. i can guess you put a good amount of time to do that for me, thank you!:slight_smile:

well said!.. i have had a similar break through accomplishments in my juggling

One of the things that some people have trouble with, is that the JavaScript code is not executed in order from top to bottom. Instead, when JavaScript comes to a function it just goes “Oh, there’s a function. I won’t go inside of it. Instead I’ll just remember what that function is called.”

It’s only later on when the function is called, such as with addingNum(2, 5), that the code inside of the function gets executed.

Following on from Paul’s comment

I have been doing a bit of searching online, ‘js functions under the hood’, ‘js execution context’ etc. and not coming up with a great deal of worth — either lightweight or I think confusing for a beginner.

I just did a search for ‘Will Sentance free tutorials’ and was pleased to find these lectures.

The first video pretty much covers the point Paul made about order of execution.

I think Will is an excellent tutor and goes into great detail. These should be helpful.

3 Likes

ok

got through the first video, took some notes

“As soon as we start running our code, we create a global execution context” please clarify?

help with “Thread of execution (parsing and executing the code line after line)”

Live memory of variables with data Known as a Global Variable Environment, help here too

also

please clarify 6:32 to finish

my plan is to finish the other 2 videos then get back to the above discussion… unless that a bad idea

I would certainly do that. I haven’t watched the third on callbacks yet (I have watched other videos of his on the topic), but I think they all tie in and will help you build up a better picture of how it all works.

i will take your advice.

any thoughts on my questions in my last post?

thanks!

A pretty thorough article here on execution contexts. It might be a little bit hard going, but it is one of the better articles I have come across — I have to say again you do need to do your own research.

When reading through it try and picture what you have seen in Will’s videos.

Just for your reference when the author refers to the variable object being an object-like container, he is referring to something like this. Objects are a common data collection type in JS.

// Variable Object
{
  message : 'Hello',
  num : 42,
  greeting : (name) { ... function code }
}

Here is another diagram — somewhat simplified — to illustrate.

1 Like

yes, you certainly put some effort into helping me here

generous

to be honest its beyond my skill set

NO WORIES! i will come back to your help when its appropriate.

as are the videos below.

i need to start (and maintain) the basic basics

this is the best way to help myself

i get

i am here, but not with other code thrown in

i watched the YouTube multiple times and i am simply not there yet

yes i will look for many more resources on functions and move onto returns, for now :slight_smile:

you may see me again

Sorry you didn’t get more out of the links and info given to you. Maybe as you say a later date.

Good luck :slight_smile:

ok, getting there

put some time studying online for this :slight_smile:

i hope the code here is correct. i copied and pasted from another resource… so i think it should be ok

// Call a function and save the return value in x:
let x = myFunction(4, 3);

function myFunction(a, b) {
  // Return the product of a and b
  return a * b;
}

can i make a

return a + b;

?

can i make a

return a - b;

?

does

return

work with the given arguments?

is there a

return

of the invoked arguments?

or are the invoked arguments the values of a

return

?

thank you for your time, and for not making me feel stupid for not grasping this concept

again, thanks!

The return is of a statement, so it doesn’t need to involve any of the parameters.

function generateRandomNumber(low, high) {
  const range = high - low;
  const randomNumber = range; // I give up here, for now
  return 3;

In that case the return value is the number 3. It’s not a useful random number generator that always generates the same number, but strictly speaking it is allowed for random numbers to all be the same. It’s just not all that common, that’s all.

The rest of the code above it isn’t needed for that return statement to just return 3, but you might admit that some more work is required there so that the function can end up fulfilling its desired intention, at which point a later development of the return statement might be:

  return randomNumber;

With, of course, some other work made to the function so that it properly generates a more random set of random numbers;

So my first thought is that the code you have copied maybe demonstrating hoisting.

What have you tried?

My recommendation. Put the copied code aside, out of view.

Have a go at writing 4 functions add, subtract, multiply and divide and see if you can make them return a result.

Some terminology, because you seem to be a bit confused.

Invoking a function

When you run a function e.g. myFunction() you are invoking it — you invoke or run a function. You can only invoke arguments if they are functions. These type of function arguments are usually called callbacks.

Function parameters

When you declare a function the parameters are the labels you put inside of the parentheses. In the following code function myFunction(a, b) { ... } a and b are the function parameters.

Arguments

Arguments are the values you pass into your function. So in the following invoked function myFunction(4, 5), the numbers 4 and 5 are the arguments being passed into that function.

I hope that helps :slight_smile:

1 Like

many thanks!

will follow through on your instructions

please, what “return to caller” mean?

in my case here