Where do Python Functions return their Values ?

Where do Python Functions return their Values ?

shomari.rivero65CK4
Explorer Explorer
326 Views
2 Replies
Message 1 of 3

Where do Python Functions return their Values ?

shomari.rivero65CK4
Explorer
Explorer

For a python function without a return statement, the function result is returned to the
function object that called it ???

Whats the difference between that and the return statement ?

327 Views
2 Replies
Replies (2)
Message 2 of 3

jmreinhart
Advisor
Advisor

I'm not sure what you are asking, but I'll do my best.

 

For a function without a return statement None is returned.

def doNothing():
    print('doing nothing')

b = doNothing()
print b

 

I think you might be asking is, "What is the difference between modifying a variable in a function to change it, versus reassigning it using the value returned from the function. There's not really a function difference for simple variable types (int, double, float), but lists and dictionaries are passed into functions as references.

 

In example 1 we pass "b" into doubleNumber and it gets modified inside the function, but **** doesn't affect the actual variable value after the function ends.

 

In example 2, because we are using a list, we aren't just passing the value into the doubleNumber, we are passing a reference to "b" so anything we do in that function affects the variable even after the function ends.

 

# Example 1
def doubleNumber(a):
    a = a*2

b = 2
doubleNumber(b)
print b


# Example 2
def doubleNumber(a):
    a.append('3')

b = [2]
doubleNumber(b)
print b

 

For stuff like this it's probably better to look at basic python information sites, rather than ones specific to Maya.

 

Message 3 of 3

Kahylan
Advisor
Advisor

Hi!

 

The return statement allows you to exactly determine which values you want to return. For example if you have a function that puts things in a list, the return statement then allows you to return that list.

 

The function result is just None or False. None means that the function ran and returned without problems. False means that an exception was raised and the function stopped prematurely. So without a specified return statement you can't give any values from locally declared variables back out of the function.

 

Return is really useful, as it allows you to store data created within function B in a variable in fuction A where you call fuction B.

 

I hope this clarifies things a bit.

0 Likes