# 4.16 Function-Call Stack¶

- A stack is like a pile of dishes.
- When you add a dish to the pile, you place it on the top.
- When you remove a dish from the pile, you take it from the top.
- Stacks are last-in, first-out (LIFO) data structures.

### Stacks and Your Web Browser’s Back Button¶

- A stack of webpage addresses supports a browser’s back button.
- For each new web page you visit, the browser pushes the address of the page you were viewing onto the back button’s stack.
- When you press the browser’s back button, the browser pops the top stack element to get the prior web page’s address, then displays that web page.

### Stack Frames¶

- Function-call stack supports the function call/return mechanism.
- Eventually, each function must return program control to the point at which it was called.
- For each function call, the interpreter pushes an entry called a stack frame (or an activation record) onto the stack.
- Contains the return location that the called function needs so it can return control to its caller.
- When the function finishes executing, the interpreter pops the function’s stack frame, and control transfers to the return location that was popped.
- The top stack frame always contains the information the currently executing function needs to return control to its caller.

### Local Variables and Stack Frames¶

- Most functions have one or more parameters and possibly local variables that need to:
  - exist while the function is executing,
  - remain active if the function makes calls to other functions, and
  - “go away” when the function returns to its caller.
- A stack frame stores the function’s local variables.

### Stack Overflow¶

- The amount of memory in a computer is finite, so only a certain amount of memory can be used to store stack frames on the function-call stack.
- If the function-call stack runs out of memory as a result of too many function calls, a fatal error known as stack overflow occurs.

### Principle of Least Privilege¶

- States that code should be granted only the amount of privilege and access that it needs to accomplish its designated task, but no more.
- An example of this is the scope of a local variable, which should not be visible when it’s not needed.
- This is why a function’s local variables are placed in stack frames on the function-call stack, so they can be used by that function while it executes and go away when it returns.
- The principle of least privilege your programs more robust by preventing code from accidentally (or maliciously) modifying variable values that should not be accessible to it.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 4 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.