eolas/zk/Call_stack.md

38 lines
908 B
Markdown
Raw Normal View History

2022-07-15 08:30:04 +01:00
---
2024-06-26 06:00:04 +01:00
tags: [memory, javascript]
2022-07-15 08:30:04 +01:00
---
# The call-stack
2024-06-26 06:00:04 +01:00
A [stack](Stacks.md) data structure that holds the information of the functions
called within a program that allows transfer of the application control from
these functions to the main process after code inside the functions has been
executed.
2022-07-15 08:30:04 +01:00
## Example
```js
2022-09-06 15:44:40 +01:00
function greet(who) {
2022-10-02 14:30:05 +01:00
console.log("Hello " + who);
2022-07-15 08:30:04 +01:00
}
2022-10-02 14:30:05 +01:00
greet("Harry");
2022-07-15 08:30:04 +01:00
2022-10-02 14:30:05 +01:00
console.log("Bye");
2022-07-15 08:30:04 +01:00
```
2022-09-06 15:44:40 +01:00
### Breakdown
2022-07-15 08:30:04 +01:00
1. Interpreter receives call to `greet()`
2022-09-06 15:44:40 +01:00
2. Goes to the definition of this function (`function greet(who)...`)
2022-07-15 08:30:04 +01:00
3. Executes the `console.log` within this function
4. Returns to the location that called it (`greet("Harry")`)
5. Finds that there is nothing else to do in this function so moves to next
function: the `console.log("bye")`
2022-07-15 08:30:04 +01:00
6. Executes
2022-07-16 10:30:04 +01:00
7. Returns to line that called it. Finds nothing else to do. Exits program.
2024-06-26 06:00:04 +01:00
## Related notes
![Stack memory](Stack_memory.md)