-1

This should be the proper method of calling a function inside another function:

def function1():
    print("Hello!")

def function2():
    function1()

function2()

Here it should give a None because I didn't return any value. Why None is not present in the output?

Hello!

I've tried this:

print(function2())

When I'm calling the function inside a print() function, why is it giving me the result with None?

Hello!
None

But when I do like this, why is it giving me the same result?

def function1():
    print("Hello!")

function2 = function1

function2()

Here I've just assigned function1 to function2. I didn't make any function2(). But when I'm calling function2(), why is it giving a result from function1()?

Hello!
Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
MONTASIM
  • 316
  • 2
  • 8

2 Answers2

3

Looking at your previous questions, I advise you to follow some basic course on Python instead of asking these types of questions.

When you do not give an explicit return statement, any function will return None. When you print a function, you print its return value. return and print are not the same.

So print(function2()): function2 calls function1, function1 prints Hello, function1 returns None (but this value is not used because it is not assigned), and function2 then returns None to the print function which prints None. So first the code prints Hello, and then None.

In your last example, you never printfunction2 so you will never print None. However, if you would use print(function2()), you would print None because you then print the return value of that function which is (implicitly) None.

If your misunderstanding is even deeper, and you do not understand the difference between print and return, then I advise you - again - to follow a beginners course on Python.

Bram Vanroy
  • 24,991
  • 21
  • 120
  • 214
1

It will print none when you use the print statement without something printable in it. Like when you print a function that does not return anything.

this works just fine because print gets a string to print:

print('hello') 

This works because print still gets a string to print, and it gets called after calling function1

def function1():
  print('hello')

function1()

What this does is first call function1, thus running the print statement inside it, but then it proceeds to print the return value of the function as well, which it doesn't have so it would print None:

def function1():
  print('hello')

print(function1())

To make this work you would have to make the function return a string for the outer print to use:

def function1():
  return 'hello'

print(function1())
Rick
  • 187
  • 2
  • 8