Strange console output

I’ve got this simple code here:

def y(a,b):


and a strange output:


why it returns “None” ?

Many thanks !

None is the value our caller will see if the function has no return value to send back.

def ab(a, b):
    return a * b

print (ab(6, 7))

Thanks, Roy!
So what it does as I understood :
It executes y(3,4) which prints y and 12.
But the print() actually just prints None because y() isn’t a return function.

But why does y(3,4) get’s executed at all? Shouldn’t Python recognize that y() has no return statement and directly return None? @mtf

1 Like

print(y(3,4)) This prints the return value of your y() function which as Roy pointed out is “None”
The function itself still executes, and prints “y” and then the result of (a*b) which is “12”
To work the way it seems you intend you could do one of two things:

def y(a,b):



def y(a,b):
  return a*b


Hope this helps.

Just because there is no return statement doesn’t mean to abort the function. All the code will run, regardless. Python doesn’t need to keep track of it. We do. None is what the caller will see if there is no return value.

Consider the function in previous example (ab()). It thas one role, which is to compute and return the product of two numbers, locally referred to as a and b.

We know that the caller will see this and be able to assign it or print it at that end.

meaning_of_life = ab(6, 7)
print (meaning_of_life)
# <- 42

print (ab(meaning_of_life, 1/7))
# <- 6.0

Notice how we still have the value from the original return? Had we only printed it in the function it would be forgotten the instant it is sent to the screen.

Make a habit of not printing when computing a value. Send it back (return) to the caller and let the program handle it at that end. Your functions will be far more versatile and the data will be in tow.


For argument’s sake, let’s say that Python equips every function with a standard return statement,

return None

but it wraps this one in a conditional so that any authored return statement will override it.

Now we can think of every function returning None if not superceded by a literal return in the code.

def func():

print (func())
# <- None
1 Like