Reputation: 305
Greetings Pythonic world. Day 5 of learning Python and I am grappling with functions that call other functions. This will be simple to some people...
In this code:
def powers_of_x(x):
print ('Powers Function with ', x, ' entered:')
for n in range (-3, 4, 1):
print ('power of ', n, "= ", x**n)
return 'First one ended (using variables with defined range)'
def powers_of_9():
print ("Powers function for 9 is:")
powers_of_x(9)
return 'Second one ended (no passed variable here)'
def combined():
x = int(input('Enter value to start:'))
print ('First, run powers_of_x function for entered value of: ', x)
powers_of_x(x)
print ('Second, run powers_of_9 function for powers of 9:')
powers_of_9()
return 'All now finished.'
The first function powers_of_x(x)
works fine on its own. So does the second, powers_of_9()
. In both cases, the printed return
message appears.
But when they are called by the third function combined()
, the final return
message from each ('First one ended...' and 'Second one ended...') is not printed. Why not? "All now finished" appears, as it should. Thank you for any corrections.
[Edited] Sorry, I might not be stating this well. My question was why the text "First one ended..." and "Second one ended..." appear when the first two functions are run separately, but not when the function combined()
runs. Is this a feature of return
? If so, I will stick to using print
.
Upvotes: 0
Views: 1119
Reputation: 10360
My question was why the text "First one ended..." and "Second one ended..." appear when the first two functions are run separately, but not when the function combined() runs.
It looks like you're running your code at the interactive prompt (either in IDLE or on the command line). At the interactive prompt, when you run a function and don't assign its return value to anything, the return value is printed to the screen. If you do assign its return value to a variable, or if the function isn't top-level, the return value is not printed to the screen. For example:
>>> def foo():
return 3
>>> def bar():
foo()
>>> foo() # not assigned to anything -> prints return value to output
3
>>> bar() # not top-level -> doesn't print return value to output
>>> x = foo() # assigned to x -> doesn't print return value to output
This is a quirk of the interactive prompt. You will not get the same results if e.g. you add the line powers_of_9()
to the end of the file and then run it. When you do that, your results look like this:
Powers function for 9 is:
Powers Function with 9 entered:
power of -3 = 0.0013717421124828533
power of -2 = 0.012345679012345678
power of -1 = 0.1111111111111111
power of 0 = 1
power of 1 = 9
power of 2 = 81
power of 3 = 729
As you can see, the return value of powers_of_9
is not printed.
In any case, the way you are using the return
statement is not correct. You should use return
only when you want to extract some information from a function for use elsewhere. For example, like this silly example:
def add(x, y):
return x+y
def print_two_plus_three():
result = add(2, 3)
print(result)
When you just want to display some information, print
is what you should use.
Also, I would like to offer some commentary on your code. To achieve the results you want, your code should probably look something like this:
def powers_of_x(x):
print('Powers function with', x, 'entered:')
for n in range(-3, 4, 1):
print('power of', n, '=', x**n)
print('First one ended (using variables with defined range)')
def powers_of_9():
print('Powers function for 9 is:')
powers_of_x(9)
print('Second one ended (no passed variable here)')
def combined():
x = int(input('Enter value to start: '))
print('First, run powers_of_x function for entered value of:', x)
powers_of_x(x)
print('Second, run powers_of_9 function for powers of 9:')
powers_of_9()
print('All now finished')
print('abc', 'def', 'ghi')
, the printed string is abc def ghi
. That is, each argument to print is separated by a space. You can alter this behavior by writing e.g. print('abc', 'def', 'ghi', sep='X')
, where the sep
keyword argument specifies what string should be used to separate arguments. As you might guess, this prints abcXdefXghi
. return
values (unless you're returning a tuple, of course). print('foo')
, not print ('foo')
. input()
, so that the inputted values are separated from the input prompt. Upvotes: 2
Reputation: 118021
Because they are being returned, not printed. They are returning a string, but in combined() those strings are not being assigned to anything.
You could say:
my_string = powers_of_x(9)
print(my_string)
Or simply:
print(powers_of_x(9))
Upvotes: 1
Reputation: 500933
You are ignoring the return values of your functions. If you want to print them, add print()
:
print(powers_of_x(x))
etc.
That said, it would seem more logical to simply change the return
statements to print()
.
Upvotes: 4