Sublime Forum

Python "return (variable)" in a function does not output

#1

I’ve been using Sublime Text 2 for a little while now.
It was fantastic and did exactly what I wanted to do.

However, recently, I wrote a function and it wouldn’t work.
So I thought something must be wrong and started using all sorts of codes and they won’t return.

In example, the usual occurrence.

[code]def greatest(a):
big =0
for i in a:
if i > big:
big = i
return big

greatest([1,2,3])

def something():
return “something”

something()[/code]

However, if I use “print” in my functions they respond alright. But I need “return” anyways.
I get:

[Finished]

[code]def greatest(a):
big =0
for i in a:
if i > big:
big = i
print big

greatest([1,2,3])

def something():
print “something”

something()[/code]

I get:

3 something [Finished]

Basically “return” isn’t working.

The return function works when I run them through the terminal.
As shown below:

[code]>>> def greatest(a):
… big =0
… for i in a:
… if i > big:
… big = i
… return big

greatest([1,2,3])
3

[/code]

Don’t have much of a lead in that sense.

Funnily, I just tried this on an online Python interpreter and that one acts just like Sublime Text 2 does as well (“return” doesn’t return), I ran it on Google Chrome.

I haven’t attempted reinstalling anything yet. I don’t see the point of going through all that hassle. Don’t recall much that could’ve caused it although as I typing Google Chrome is looking like the culprit.

I am operating on latest release of Slackware 13.37 32-bit.

Any ideas? Anyone been through it?

0 Likes

#2

You are running into the difference between executing a program and running in interactive mode. In interactive mode, if you “return” something from a function, and then call the function from the interpreter, it displays the thing returned. However, when you execute a program, the interpreter simply runs each line of the program. It is not expecting to display anything unless it is explicitly told to using the print function. Items “returned” from a function are thrown away unless you actually USE them: assigning the result to a variable (x = something()), using print to display the result (print something()), or using the result in another operation (something() + " somewhere").

The behavior you are seeing is exactly what is intended. If you want the result output, you need to use print as I showed above.

0 Likes

#3

Exactly, so in the context it will be:

[code]def greatest(a):
big =0
for i in a:
if i > big:
big = i
return big

print greatest([1,2,3])

def something():
return “something”

print something()[/code]

0 Likes

#4

Holy.

facepalm

That was utter noobness from my side.

I was simply calling my functions wrong when I wanted to test them.

But yeah I actually didn’t know that.
I thought something could’ve been wrong with all the editors I was using because I was getting them through in the interpreter.

I guess it was more a Python question.

Can’t believe I got this far with using it before realizing I had to “print” the functions. I thought it was optional in a sense.

Thanks a lot.
Greatly appreciated.

0 Likes