In the following example, every time the function add gets executed, a brand new execution context is created and after the execution of the function is completed, the state is deleted without being able to ever access state from previous functions run again.
However, it might be useful to access state from previous runs of the function later on in our programs, for example in case we need to rely on previous execution state of the function and on previous state.
The following code will do the sum of numbers by storing the result in the variable sum, which is stored in Global Memory as a “backpack”. Will defines in his course a “backpack” as being all the surrounding data that the function brings with it after its execution into the Global Memory.
So what exactly happens when we run this code? I will explain all the steps that happen during code execution.
I will start executing the code line by line and I will describe what happens during code execution with the help of the memory diagram and I will refer to (step X) when addressing the memory diagram .
At line 1 the function sum is defined in the Global Memory (step 1), not knowing at this moment yet what will be the result of executing the function sum. The execution of the program jumps to line 11 where the constant addNumber is defined. In the Global Memory (step 2) addNumber is uninitialised for now until the execution of sum is finished.
A brand new execution context is created (step 3) and the execution of sum starts. In the Local Memory a new variable sum is defined which is initialised with 0 (line 2). Then the function add is defined and it is returned (line 8). The constant addNumber is now initialised with the function add that was born by executing the function sum, having the variable sum initialised with 0 in the variable `[[scope]]` stored in Global Memory as a “backpack” to the addNumber constant (step 4).
At line 12 a constant number1 is defined, which remains uninitialised for now (step 5), but will be the result of executing the method addNumber with parameter 3 (step 6). When the function addNumber is executed, a brand new execution context is created. The sum variable is searched in the Local Memory, it is not found there, then it is found in the [[scope]] of addNumber which is its “backpack”. sum is updated to 3 in the “backpack” and number1 is initialised with 3 (step 7).
At line 13, the constant number2 is defined (step 8), which remains uninitialised until the execution of addNumber (step 9) finishes. A brand new execution context is created, where num is initialised to 6 and sum is returned with the value 9 into the variable number2 and the value in the “backpack” of addNumber is updated to 9 (step 10).
At line 14 and 15 values 3 and 9 are printed into the console.
Vezi toate oportunitatile disponibile la Metro Digital aici
Alte articole din aceasta sectiune:
- HR in pandemie - cum ne dam seama ca echipele noastre sunt bine dincolo de ecrane
- WEBCAST: CIO TALKS - Powered by CIO Council & CARIERE ”Digital first. The new era of hybrid workplace”
- DevTalks Reimagined revine online, pe 9 – 11 iunie, cu scene noi dedicate liderilor din industrie, tehnologiei 5G si ingineriei
- Gitte Mønsted Hansen - Senior Manager, People Attraction at Systematic - We are a modern company with a Danish foundation and a global mindset
- Webinar How to adapt to changes in the online world for your career - Top Talents event
- Responsabil Clientela Persoane Fizice - Aiud Raiffeisen Bank
- Ofiter de Cont - Marghita Raiffeisen Bank
- Receptionist Policlinica Orhideea - Part Time Regina Maria
- CyberSecurity Incident Analyst for Electronic Controls Vitesco Technologies
- Farmacist/Referent farmacolog-Fieni Help Net Farma