Dangermoth
Posts: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

returning values from functions

Fri Nov 23, 2018 7:07 pm

Afternoon all,

OK, flame suit on, I sure someone can help with this one.

I've written quite a few PRI programs such as graphical Pygame weather stations that have been running a treat for many months but I've always been limited as I have never been able to return any sort of returned value from a function so every one of my functions has to be self contained.
I come from a C background (Arduino, PIC etc)where any declared value can be shared between any function but nothing seems to work in python, and everywhere I've searched doesn't give an understandable answer. Lots of people seem to struggle with this, its so annoying as this is about as basic as it gets.

This is a simple bit of test code to try to figure out what is going on

Code: Select all


value = 0	#declare value
result = 0	#declare value

def maths(value):#                   function
    result = value  + 50
    print result#                            this prints 150 showing that 100 has been successfully passed to the function
    return result

maths(100)#                         call function and pass value of 100 to it

print result#                              print the result, this prints 0 as originally declared and not 150 as printed within the function


I would expect a value of 150 to be printed but it prints 0.

What on earth am I doing wrong??

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: returning values from functions

Fri Nov 23, 2018 7:13 pm

Try this:

Code: Select all

result = maths(100)
print result
Last edited by buja on Fri Nov 23, 2018 7:16 pm, edited 1 time in total.

Andyroo
Posts: 4188
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: returning values from functions

Fri Nov 23, 2018 7:15 pm

The 'return' command tells python what to pass back in the function name so in the example below you would need:

Code: Select all

value = 0	#declare value
result = 0	#declare value

def maths(value):#                   function
    result = value  + 50
    print result#                            this prints 150 showing that 100 has been successfully passed to the function
    return result

print maths(100) #                         call function and pass value of 100 to it
By the way - you do know you are using Python 2 rather than Python 3 - better to move up :lol:
Need Pi spray - these things are breeding in my house...

Dangermoth
Posts: 59
Joined: Wed Oct 25, 2017 7:34 am
Location: Sheffield

Re: returning values from functions

Fri Nov 23, 2018 7:24 pm

Thanks for the quick reply guys, I just now found adding 'global result' after the 4th line seems to work as well, I guess this is how I make the variable a global although I believe thats frowned upon in python.

I did try python 3 but had too many issues, the main one being that rpi_backlight refused to work.

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: returning values from functions

Fri Nov 23, 2018 7:30 pm

There are two different variables with the same name "result".
The one that is initialized to 0 in line 2 is a global variable. It is never updated, so that's why it prints as 0.
The one used inside the function "maths" is interpreted a a new, local variable. It's value is returned as the value of the function.

The confusing bit is the automatic definition of the local variable in Python, in C that is much clearer.
On the other hand: the use of global variables is a well known source of trouble and should be avoided as much as possible. This is true for every programming language, not only Python.

Edit: actually, after looking it up, Python works just the other way around than C with respect to local and global variables. C: global unless a local variable is defined. Python: local unless specified as global.

User avatar
bensimmo
Posts: 4156
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: returning values from functions

Fri Nov 23, 2018 7:35 pm

Post the code up that is not working in Python3 and someone may be able to update it for you (if you don't know how).
But either way

Use
print(xxxx)
And not
print xxxx

As both now work in Python2.7, but one the () version works in Python3

User avatar
bensimmo
Posts: 4156
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: returning values from functions

Fri Nov 23, 2018 7:41 pm

You don't declare the variable (most of the time).

Code: Select all


def maths(value):#                   function
    result = value  + 50
    print (result)#                            this prints 150 showing that 100 has been successfully passed to the function
    return result

value_now = maths(100)#   assign value_now the returned value of of the function and pass value of 100 to it
print(value_now)#
#or
print(maths(200)) #to do it directly from print BUT no variable is available for further use.


Return to “Beginners”