Won’t stack diagrams become really long and extensive the more functions we have?

Yes; we don’t usually use it for the entire program if we are doing it by hand. We usually isolate the parts of our program we are having difficulty understanding. For our purposes, I will limit the complexity of our examples to isolate particular topics. For example, I wanted to focus on scope with the example that we did today. We’ll use it again when talking about mutability next week, and then recursion and objects in subsequent weeks.

does writing multiple functions take up more space than just writing one function (in terms of size of file)? if so, if that negligible or should we try avoiding writing too many functions?

It depends. Reusability means that we avoid repetitive code, so we should save lines of code with functions. On the other hand, we have to think about comments and defining parameters and return values. However, this is negligible. File sizes for code are incredible tiny relative to the amount of memory we have to store it.

Wow do you know what the stack diagram looks like with more than 2 functions?

Great question! We’ll definitely see examples with 3+ functions. The procedure outlined scales to any number of functions on the stack. We are always only paying attention to the function at the top of the stack; if a function gets called, it gets pushed to the top of the stack.

I’m wondering how to recall a function in the case that the user inputs invalid text

This is a strategy known as recursion which we’ll learn later in the semester. It is incredibly useful, but way more complicated than just “restarting” the function. A while loop is the best solution for this particular problem.

Are stack diagrams something you tend to do on a board while troubleshooting your own code or do you use them in other situations?

We also have software tools that do the stack diagram for us; almost anyone doing careful software engineering needs to be familiar with these tools. To start, it is easier to learn how to do it by hand and then subsequent courses introduce the software tools.

How can you get an input off the internet

https://stackoverflow.com/questions/15138614/how-can-i-read-the-contents-of-an-url-with-python

Do we physically erase stacks on top in practice?

Or cross it out; as long as it is clear that is not longer live.

I still don’t fully understand why we have to print all of the parameters of adddnumbers before we print the same thing in main

We don’t (and probably wouldn’t in practice). This was to help us understand parameters and arguments and the respective scopes of each method.

What exactly is a scope, and how is it used in a program?

Scope is how Python determines when certain variables are available. Here is an analogy - the scope of Parrish Hall is Swarthmore College. If I travel back home to the University of Michigan and go to somebody and say, “Hey, where’s Parrish Hall?”, I’m going to get a funny look because there is no such location. The same happens with functions; if I call a function, and try to access a variable that isn’t in scope, I’m going to get an error. If we think about it, the alternative is messy (all variables are in scope everywhere). You may accidentally overwrite a variable that was created else where. Or, you spend hours trying to find the origins of a variable because you work on a project with millions of lines of code.

For a stack function, do we always erase the stack when we’re done?

Yep! The function goes away from the current run.

How exactly do the return statements work and communicate between functions?

This is a topic in CS31 (our systems course) - it is many levels below the Python programming language. Luckily, we can abstract these complex details because we are guaranteed the behavior will work.

can we make a global variable only semi-global, so that it still works within most functions? Is this a bad practice, still, in this case?

I think so? For example, libraries will often define constants as global. Take the math library. It defines a variable pi that we can access after importing. Thus, pi is somewhat global because we can use it anywhere as long as we use import.

In what instances are stack diagrams useful? Are stack diagrams only used to debug a chunk of code?

Debugging and understanding are not trivial - we will see it is incredibly useful as our topics get more advanced.

Is there an agreed upon limit in CS to how many stacked codes you can have before it becomes too much?

We are simulating what the computer is actually doing. Your PC creates a stack to execute all code. Technically, the limit is the amount of memory on the machine.

Who came up with stack diagrams?

This is how we illustrate how a computer actually processes code. It isn’t far off, and abstracts the binary representation. I think it is a natural extension of what the programming language designers came up with and it varies slightly based on the programming language being used.