[Solved] Python 3: Theoretical question about the use of variables in functions [closed]


In order to give a complete answer as to why and what is happening here, first we need to talk about the concept of “scope”.
Each function (and for loop, while loop etc) “lives in its own world” in terms of memory. What does that mean? Assume I have a function named “foo1” and another function named “foo2”. Within foo1, I have a variable named x. From within foo2 – I cannot access that variable (x), since it “lives” in the scope (“world”) of foo1.
In python, you have one global scope (the.py file itself) and a scope for each function. A variable created in a scope that is not the global scope is called a “local” variable.
This brings us to function arguments – in your case, the variables named “book” and “price” in the “bookstore” function. Note – these are local variables in the scope of the function “bookstore”. How do they get their values? From the function call!
In you case, within the “print” function you called “bookstore” with 2 arguments – “book_entry” and “price_entry”. When you did this, you copied the content of those variables to the scope of the function and gave them a new name – “book” and “price”, according to the order you sent them to the function “bookstore”.
Then, from the function you return a string.
So, what happens is the computer first executes the call to “bookstore” and then executes “print” with what “bookstore” returned.

solved Python 3: Theoretical question about the use of variables in functions [closed]