Printing return value in function, Python - python

When I print the value that my function returns, it has strange characters.
If I print the value that I care inside the function, I obtain the right value: 0.653594771242
If I print the value that the function returns, I obtain:
function alpha at 0x05870630
def alpha(v1,v2):
a=(v1,v2)
b=1/sum(a)
print(b)
return b
alpha(0.817,0.713)
print(alpha)

This is because you are printing an object i.e a function. Functions in python are objects.
If you want to print the value returned by the function then this may help you.
print(alpha(0.817,0.713))

This way would probably make more sense to you.
def alpha(v1,v2):
a=(v1,v2)
b=1/sum(a)
print(b)
return b
result = alpha(0.817,0.713)
print(result)
This way the function is returning the value to result then you are simply printing the result.

Instead of print(alpha) you need to write
print(alpha(0.817,0.713))

Related

how can i use the output of one function to another function?

Let's assume I have two functions
def seq():
#here I wrote a code that evaluates the mean of a value from a csv file
print(x)#assuming the condition in the above code is true it prints x
seq()
and
def lenn():
p=4
d=#I want this variable to be the value that the 1st function produces
x=d/p
lenn()
One produces an integer and the other uses the output of the 1st function and then divides it with an integer to produce its own output. How do I call the function?
I tried calling the function name but when I tried to divide the function name with an integer it keeps saying that I have a None type. I also tried to put the 1st first function inside the 2nd function but I had the same problem.
How can i solve this?
Don't use print but return (print has no return value, so this defaults to None):
def seq():
return int(input())
def lenn():
p=4
d=seq()
x=d/p
return x
print(lenn())
The problem is that seq does not return the inputted value (x). Anyway, I wouldn't place int(input(x)) in its own function. You can try something like
def lenn():
p=4
d=int(input())
x=d/p
return x

Get returned value of function in function which called it

I am newbie in python, and I build two functions in which, I am calling second function with 1 parameters in first function and I am trying to access second function's returned data in first function.
def second_function(first_param):
final = first_param + 50
return final
def first_function():
second_function(50)
# trying to access second_function's returned data HERE
print(second_function)
But it is not showing any returned data.
Any help would be much Appreciated. Thank You in Advance.
The problem here is that you are using print(second_function), so that will simply output the name of the function. Now, if you want to output the result of the function, you should do:
def second_function(first_param):
final = first_param + 50
return final
def first_function():
output = second_function(50)
print(output)
you could first put the returned value in a variable like this
def second_function(first_param):
final = first_param + 50
return final
def first_function():
value = second_function(60)
print(value )
or print the returned value with out using any variable
def second_function(first_param):
final = first_param + 50
return final
def first_function():
print(second_function(50))
That's because second_function is an object in its own right. Try either of the following:
def first_function():
out = second_function(50)
# trying to access second_function's returned data HERE
print(out)
def first_function_alternate():
print(second_function(50))
What's happening when you do print(second_function) is that the computer is trying to print the value of the function itself, not what it returns. We can store this value to a variable (my first answer) or simply generate it on-the-fly (my second answer).
In Python, the returned data from a function will be assigned to a variable. So you would use:
my_value = second_function(60)
and the returned value would be stored in the variable my_value

Why is the function printing the first two lines before calling it?

Very new to python and I'm learning about defining and calling functions.
When def function4(x): and then define its output as a variable m, it prints the first two lines of the function before I even call it. Then when I call the function it only displays the return value. I was under the impression that anything indented under def function4(x): would not be executed unless function4(x) was specifically called?
Example:
def function4(x):
print(x)
print("still in this function")
return 3*x
m = function4(5)
print("BREAK")
print(m)
Output:
5
still in this function
BREAK
15
Process finished with exit code 0
Thanks for your time!
You're right, the function will not execute until you call it. You are calling it, though, right here:
m = function4(5)
So your print statements are executing in exactly the right place. You are setting m to the value returned by function4(5).
print does not call anything. It simply prints the string representation of whatever you give it to the console:
# a simple function to demonstrate
def f(x):
print("I am ", x)
return x
# I have not called f yet
print('Hello! ')
# I have printed the *function* f, but I still have not called it
# note the lack of parentheses
print('Here is a function: ', f)
print('We will call it now!')
# *Now* I am calling the function, as noted by the parentheses
x = f(1)
print('I have returned a value to x: ', x)
Which will do the following:
Hello!
Here is a function: <function f at 0x7fa958141840>
We will call it now!
I am 1
I have returned a value to x: 1
First of all I strongly recommend you that you use http://pythontutor.com/javascript.html#mode=edit if you want to know how your code is working, is very useful if you are new on Python.
Then, as for your code, you are calling the function when you declare the m variable, so that's the reason why the two print statements appear first.
The return value will only appear if you print the function, that's why the number 15 appears after all because you've printed it when you wrote print(m).
Hope this can help you, Goobye then and goodluck!.

Return statement in python function not returning anything [duplicate]

This question already has answers here:
Python: Return possibly not returning value
(5 answers)
Closed 3 years ago.
I don't understand the difference between return and print. I was told that you should use return in function statements but it doesn't return anything, so I've been using print in my functions. But I want to understand why return statements in my functions do not work.
def triangle_area(b, h):
return 0.5 * b * h
triangle_area(20, 10)
I would expect this return the multiplication, but it doesn't yield anything. Please help! And when I replace the return with print, it works perfectly. Am i supposed to use print in functions?
return and print are two very different instructions. Return has the purpose of storing the value that is computed by a function, value that will be used according to the programmer's wish. Print does what it says: it prints whatever you tell him to print.
Using return won't print anything, because this instruction is not built for such a purpose. Only for storing. Nothing more, nothing less. print displays something on the screen.
Like this:
print(triangle_area(20, 10))
Good luck to you all, bro!
It does return the product; what it doesn't do is print it (since you didn't tell it to).
Because you are not printing the return result from the function. Please try:
print(traingle_area(20,30))
You are not receiving the output the function is returning.
print(triangle_area(20, 10))
This will show the result.
It is returning the value, you simply are not capturing it or doing anything with it. For instance:
def triangle_area(b, h):
return 0.5 * b * h
output = triangle_area(20, 10) # capture return value
print(output)
Or as others have suggested, just pass the result straight to the print function if that's what you're looking to do:
def triangle_area(b, h):
return 0.5 * b * h
print(triangle_area(20, 10)) # capture return value and immediately pass to print function
Note that the output will be lost if passed straight to print(). If you want to use it later in your program, the first code block is more appropriate
Functions return a value, so you need to store them in variable.
print(triangle_area(20, 10))

Function returns as error (python 3)

I am kinda new to Python, and I would like to ask a question :
def spam(a):
a = 1 + a
return a
spam(21)
print(spam)
input()
After running it, the output is function spam at 0x021B24F8. Shouldn't the output be 22? Any help will be appreciated.
The problem is that your function, i.e. spam is returning a value. You need to accept the value returned by the function and store it in a different variable as in
s = spam(21)
print(s)
Here, you will store the returning value in the variable s which you will print it out.
After making the correction, the program will print as expected as in
22
Note - As mentioned, having a single statement print(spam(21)) also works as spam(21) will return 22 to the print function which will then print out the value for you!

Categories

Resources