def mult_x_add_y(number, x, y):
print(number*x + y)
result = mult_x_add_y(1, 3, 1)
print(result)
What is wrong in this set of code?
I thought I would get the output as :
4
4
But i got it as :
4
None
Why did this happen?
def mult_x_add_y(number, x, y):
print(number*x + y)
result = mult_x_add_y(1, 3, 1)
print(result)
What is wrong in this set of code?
I thought I would get the output as :
4
4
But i got it as :
4
None
Why did this happen?
Saharshjain, make sure to use the </> icon to post your code properly formatted, so that we can read it easily, and it is actually Python code. I need to guess a bit about the indentation. Is this it?
def mult_x_add_y(number, x, y):
print(number*x + y)
result = mult_x_add_y(1, 3, 1)
print(result)
Let us follow the code. The Python interpreter goes down the left-most indentation level.
First it sees the function declaration def mult_x_add_y():
. It puts the function in memory, keeping the function name for reference.
Next, it gets to the line result = mult_x_add_y(1, 3, 1)
That is an assignment statement (because it has the assignment operator, =). Every assignment statement says, "first, evaluate the expression on the right, then assign the returned value to the variable on the left.
So, the expression on the right is the original function, along with the arguments (1, 3, 1). The function is called, and it is executed: It has one line, print(number*x + y)
and we know because of the order of the arguments that number = 1, x = 3, and y = 1. The expression within the print() function parentheses evaluates to 4, so you see 4 printed on your screen.
Now, here is the key: The function has executed, and the next step is to assign its returned value to the variable result. What is the returned value?
==> There is no returned value specified for this function, and in Python, if a function has no return value specifies, it returns None.
So the value None is assigned to the variable result.
The next (and final) line is print(result)
. When this is executed, None is printed, onscreen, as you see.
Bottom line:
Okay…I now know what went wrong. Now,
I tried to get the out put as :
4
4
By adding a return statement at the end of the function. So the code is :
def mult_x_add_y(number, x, y):
print(number*x + y)
return mult_x_add_y(1, 3, 1)
result = mult_x_add_y(1, 3, 1)
print(result)
When I executed this I got an infinity loop having an output of 4s
What should i do now?
What went wrong?
Don’t forget to use the </> icon to properly display code. Is this it?
def mult_x_add_y(number, x, y):
print(number*x + y)
return mult_x_add_y(1, 3, 1)
result = mult_x_add_y(1, 3, 1)
print(result)
Notice that when return is called, the function runs again, that is, the function calls itself.
That is called recursion, and if you delve further into computer science, you will see that it can be a very useful technique. But at this stage, it is best avoided, for it is nearly a certain way to ensure an infinite loop.
(Unless you are purposely using recursion - which should not be the case here), return a value or an expression that produces a value, not the function that contains the return statement…
Done…
def mult_x_add_y(number, x, y):
new_number = number * x + y
return new_number
new_number = mult_x_add_y(1, 3, 1)
print(new_number)
I got the answer as 4 at last
Thanks so much patrick.
But I still can’t understand how to properly display code
I got it…“stetim94” helped me
def myfunction():
return 3+3
print(myfunction())
the above program prints 6…but if we call the function instead of using print ,there is no output…like below program doesnot print the output
def myfunction():
return 3+3
myfunction ()
any body explain pls
TIA
myfunction returns the value 6. But the value is not stored into any variable
you can try this:
def myfunction():
return 3+3
myfunction_output = myfunction()
print(myfunction_output )
Perfect Summary ! thanks
That depends what you want your function to do, actually when a function returns a value it uses return keyword, and the value or values are passed on
But when printing it uses print() function and it prints it where it is used.
What’s happening is:
When print(result) is printed it prints the value of result here 4
But, since the function is returning Nothing, by default in Python a None is returned, now as you are calling the function a None is returned, that’s why the ‘None’ is printed on next line after 4.
To get:
4
4
return result, should be written at the end of function mult_x_add_y(number,x,y)
Brilliant Explanation…This is what I was looking for all these days…How the program gets executed…