Should my function use print or return?

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:

  • We print() values that we want to see onscreen
  • We return values that Python needs to use, possibly to assign to a variable, or pass to another function.
4 Likes

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…

4 Likes

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. :grin:

But I still can’t understand how to properly display code :confused:

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