eolas/Software_Engineering/Call_stack.md

32 lines
894 B
Markdown
Raw Normal View History

2022-07-15 08:30:04 +01:00
---
2022-09-06 15:44:40 +01:00
category: Software Engineering
2022-07-15 08:30:04 +01:00
tags:
2022-09-06 15:44:40 +01:00
- callstack
2022-07-15 08:30:04 +01:00
---
# The call-stack
A [stack](/Data_Structures/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.
## Example
```js
2022-09-06 15:44:40 +01:00
function greet(who) {
console.log('Hello ' + who);
2022-07-15 08:30:04 +01:00
}
2022-09-06 15:44:40 +01:00
greet('Harry');
2022-07-15 08:30:04 +01:00
2022-09-06 15:44:40 +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")`)
2022-07-16 10:30:04 +01:00
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.