Regarding function call

def sing_song():
  print("You may say I'm a dreamer")
sing_song()

I understand that the function call by itself will execute the code inside the function, and would print “You may say I’m a dreamer”.
But when I call a function call inside print() as shown below, the output is “You may say I’m a dreamer None”. I don’t understand the logic of None in this print statement.

def sing_song():
  print("You may say I'm a dreamer")
print(sing_song())
1 Like

print() is a function. It prints the argument passed to it. If you pass a string, it prints the string. If you pass a function call, it prints the return value of the function. A function that doesn’t explicitly return a value, implicitly returns none.

def sing_song():
    print("You may say I'm a dreamer")
    return "I'm not the only one."

print(sing_song())

Output:

You may say I’m a dreamer
I’m not the only one.

To avoid printing the implicitly returned none, you could just call the function:

sing_song()
3 Likes

You are getting None in your second function call because you did not return anything to your function. In Python 3, a print statement is a function. It will execute statement inside sing_song function, and then start looking for other code to execute but there is none since you did not return anything back to sing_song. In order to avoid this, always use return in your function statements if you are going to use print in your function call.

def sing_song():
  return "You may say I'm a dreamer"
print(sing_song())

I hope I was clear in my explanation.

1 Like