Variable scope

#function definition
def f(x):  #x is a formal parameter
  x = x + 1
  print('in f(x), x =', x)
  return x

x = 3
z = f(x) #x is the actual parameter of this function call. return value is assigned to z
At the instant when execution is at the start of f, there are two scopes: global scope and f scope. The global scope maps f -> some code, x -> 3 and z is currently undefined. In f scope, x -> 3

At the instant when execution is at the end of f: The global scope maps f -> some code, x -> 3 and z is currently undefined. In f scope, x -> 4

At the instant when execution has returned from f and z has been assigned: there is only one scope, which is the global scope. The global scope maps f -> some code, x -> 3 and z -> 4. Notice that z is assigned the value returned (using the return keyword) by f.

Consider the following (buggy) function that does not contain a return statement:

#function definition
def f(x):  #x is a formal parameter
  x = x + 1

x = 3
z = f(x) #x is the actual parameter of this function call. return value is assigned to z

return vs. print
return print
return only has meaning inside a function print can be used outside functions
only one return executed inside a function can execute many print statements inside a function
code inside function but after return statement not executed code inside function can be executed after a print statement
has a value associated with it, given to function caller has a value associated with it, outputted to the console