Advanced Function help

I am confused on some parts of functions, like return, and scope and stuff, any advice?

https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/lessons/intro-to-functions/exercises/scope

Hello @waluigithetacostandg, I will explain return and scope:

return:
The return keyword basically hands the data back to the caller, which means when you call that function, it will essentially have the data in the return statement. The return also ends a function, so no code after it will be executed:

def function1():
   return "Hello world!"
   print("Won't be printed")#This won't be printed, as it comes after a return
#but is still inside a function

The phrase “Hello world!” can now be stored in any variable, or acted upon as though it were that data type:

somevariable = function1()#now stores "Hello world!"
print(somevariable)#This prints "Hello world!", because that
#is what the function returns

Scope:
Scope is basically where a variable lives, and where it can be accessed. There are two main scopes in Python: function scope and global scope.

Global scope can be accessed anywhere in the program, while function scope can only be accessed in the function it was defined in:
Global:

var1 = "hi"#Has global scope.
def function():
   return var1#This returns "hi", as var1 is global scope, and can be accessed anywhere in the function

Local (or function) scope:

def somefunction():
   var2 = "hey"
   return var2#returns "hey", because this variable was created
#in this function

print(var2) #throws an error, as you are outside the function
#and this variable is locally scoped, which means it is not accessible outside of
#its function

I hope this helps!

3 Likes

That was a prefect explanation. Thank You.

1 Like

Global variables in Python aren’t quite as straightforward as they may seem. For example:

var1 = " you"
def somefunction():
   var2 = "hey"
   var1 = " there!"
   return var2 + var1

print(somefunction()) #prints hey there!
print(var1) #what will this print?

Also:

var1 = " you"
def somefunction():
   var2 = "hey"
   var1 += " there!" #throws UnboundLocalError: local variable 'var1' referenced before assignment 
   return var2 + var1

print(somefunction())

Trying to alter what seems to be a global variable doesn’t work in the above case. var1 inside the function is not the globally scoped var1 declared outside of the function. It is a new function scoped variable that happens to have the same name. In order to reference the global var1 we have to tell the computer that’s what we’re doing using the global keyword:

var1 = " you"
def somefunction():
   var2 = "hey"
   global var1
   var1 += " there!"
   return var2 + var1

print(somefunction()) #hey you there!
print(var1) # you there!
1 Like

But please, don’t ever do this:

Just pretend global doesn’t exist for a few years.

See my discussion of this here.

Also, just for clarification, you don’t need to use global to access a global variable inside of a function. That is only if you want to alter the global variable inside your function.

2 Likes

Right. Changing my first example will show that:

var1 = " you"
def somefunction():
   var2 = "hey"
   return var2 + var1

print(somefunction()) #prints hey you

I don’t* like to see or use the global keyword personally. I was just pointing out that what seems like it should be easy enough to do given some other languages, altering a global variable from within a function isn’t completely straightforward.

*Edited 5/20/2020 to clarify. I had left out the word don't. :man_facepalming:

1 Like

It’s totally valid to use global in small programs or if you know exactly what you’re doing. I just try to caution all beginners against it because it’s too easy to get used to using it and then when you’re writing a larger program it comes back to bite you in the butt.

Also, I think beginners tend to use it as a crutch rather than using more appropriate structures (such as dictionaries, etc., which are mutable in place).

1 Like

I totally agree. I like to keep everything as tightly scoped as possible.

1 Like