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
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:)
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.
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.
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.
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.
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
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.