The function square
never changes the variable squared
that is found in the global scope. Inside the function you declare a local variable with the same name as the global variable, this however will change the local variable and not the global variable. Then when you print(squared)
you are printing that unchanged global variable which is still 0 as you initially set it. As a matter of code cleanliness you really should try to avoid having local variables and global variables sharing the same name as it causes confusion (as we have seen in this question) and makes the code much harder to read.
To change the global variable from within a function you must tell Python to do so by using the global
keyword to make the name refer to the global variable. You might want to look at this question: Use of "global" keyword in Python
The easier and better option of course is just to use the return value of the function. Minimizing the use of global mutable state is a good goal.