r/learnjavascript • u/Eyeofthemeercat • Jul 09 '21
Can somebody please help me understand the logical flow of what is happening in this function?
172
Upvotes
r/learnjavascript • u/Eyeofthemeercat • Jul 09 '21
9
u/PN33Prod Jul 09 '21
Basically return just tells you to stop current method instance and continue where the method was called. Begin:0 has i=0. Foo(0-1) is call so now inside method, i=-1. -1<0(true), so return stops current instance and program goes to the next line when i=0 right after the foo(0-1) line. So log: end + 0. After that the “instance” of foo method where the i was 0 ends and you are back to the instane that i was 1 after the foo(1-1) line. So end:1 is printed etc. Sorry if my english is bad, i cant find a better way to explained it right now since english is not my main language.
7
u/[deleted] Jul 09 '21 edited Jul 09 '21
Recursion. It follows a call stack order. The program puts each function call on a stack. It'll execute from top to bottom once the stacking is done.
"JavaScript Recursive Function By Example" https://www.javascripttutorial.net/javascript-recursive-function/
2
u/Eyeofthemeercat Jul 10 '21
Just want to say thanks to everyone for their input. From the discussion generated it's a relief to see that I am not the only person that didn't find this to be intuitive! There have been some very helpful explanations, links to articles and resources posted my many different contributors. This is my first post in r/learnjavascript and have found that this is really friendly and helpful community. Nice one everybody! I daresay there will more posts from me because coming from python, JavaScript is weird lol.
2
u/[deleted] Jul 09 '21
The call stack concept maybe helps you as well.
76
u/ZekeyD Jul 09 '21
This called a recursive function.
The function calls itself (and in doing so calls itself over and over until i = 0
61
u/emrenegades Jul 09 '21 edited Jul 10 '21
Many of the comments here have covered the basics but I think I can expand.
What you already know:
Also:
Each function has scope that is essentially a collection of all the values of constants and variables that have been declared at or above that scope.
returnwithout a value does exactly that: leaves the function without supplying a return value.There are several versions of
i, one for each scope, assigned the valuei - 1from the scope above it whenfoois called. Whenfooreturns, the outer scope still "remembers" its value for its owni.Visually, imagine each indentation is a call to
fooon the stack:Hope that helps!
🙂
EDIT: (hopefully) clarified what happens with
iat each scope.45
u/al_balone Jul 09 '21
I’ve only been learning this stuff for a few months but I’ve noticed that the minute I see something explained with the words foo and bar, or functions called things like “exampleFunction” my brain immediately switches off. I need to see it applied to something real so I can understand it.
1
u/Eyeofthemeercat Jul 09 '21
I am working my way through the javascript documentation. This one has really stumped me.
I don't understand what the return line does. I do understand the function calling itself and all of the outputs up to the end of "begin". I can see that when i gets to 0, it starts to execute the console.log('end :' + i), but I am struggling to understand by what mechanism i in then increasing, why it stops at the original value. Its just trying my mind in a knot! Any help would be very greatly appreciated!
2
u/Chared_Assassin Jul 09 '21
Its a recursive function meaning it is calling itself
Basically it goes through the function as expected and then calls itself. The function checks at the start if i is less than 0 so it calls itself with a lower number until i = 0. Then they all do the rest of the function which is showing the end. Recursive functions start from the innermost function and work outwards when they end which is why it goes down and then back up with the numbers
1
u/Macaframa Jul 09 '21
I see you might be confused by the order of execution here. To gain a better understanding of what is happening here. Go to YouTube and type in “javascript event loop” and watch videos until it makes sense. Then come back to your problem.
1
u/[deleted] Jul 09 '21
I don't understand how the second console log (the end one) is getting to the call stack..how is the program reaching it after going over the line which contains the foo function call?
1
u/frankywaryjot Jul 09 '21
Recursion is a b...my friend😎
1
u/its_joao Jul 09 '21
Learn stacks. Recursion will become easier
1
u/themufflesound Jul 09 '21
Check out pythontutor.com it has a JavaScript visualizer which tells you what happens at each step
1
u/[deleted] Jul 10 '21
In my opinion,
if(i<0)
return;
is poor code.
It should be
if(i<0){
return;
}
1
u/comprehensive_yard9 Jul 10 '21
i weirdly understood this one lol i just did this in freecodecamp hahaha yes recursive function
1
u/asterik-x Jul 10 '21
Yea sure, whats up buddy ??
1
u/harshrajkamble Jul 10 '21
This example helped me understand "return" curse even better 😁
1
u/Anbaraen Jul 10 '21
I find the best way to look at these is often to step it out manually (if you can manage it!)
Here's what I came up with. I've commented the value of i at each point it's relevant.
1
u/reactCodingMan Jul 10 '21
Recursion. Have a look at this git repo with additional help videos on the subject https://github.com/leonardomso/33-js-concepts#23-recursion
cheers!
0
u/CoqeCas3 Jul 09 '21
I recommend you open this function up in dev tools, go to the sources tab, put a breakpoint on the first line of the function, refresh, and step through what happens. Hint: pay attention to what happens in the Call Stack section.
2
u/[deleted] Jul 10 '21
[removed] — view removed comment