-3

I wrote a function that takes in a number and prints its square. When I used the print function and passed the function-with 2 as its input- as the print function's argument, the output was 4 and 'None'.

'None' I can understand, but why did Python execute the function and output 4 here? I had only used a print statement.

Sayse
  • 42,633
  • 14
  • 77
  • 146
harry
  • 157
  • 7
  • 3
    Is there a reason you didn't include the code you're having issues with? – Sayse Jan 12 '21 at 22:10
  • StackOverflow won't let me; I've been trying for a long time and I can't get it formatted right. It's the simple, obvious code for what I've described, nothing more. – harry Jan 12 '21 at 22:13
  • `print(foo(5))` will execute `foo(5)` and then print the returned result. – John Gordon Jan 12 '21 at 22:14
  • Just paste your code into your question, highlight it and press ctrl + k – Sayse Jan 12 '21 at 22:14
  • 1
    Does this answer your question? [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) – Coder Jan 12 '21 at 22:15
  • 1
    `why did Python execute the function and output 4 here` Because _that's what you told it to do._ If you had posted your code, it would be a lot easier to explain this. – John Gordon Jan 12 '21 at 22:17
  • @Sayse: I've been doing exactly that on both PC and Android for hours now. – harry Jan 12 '21 at 22:17
  • Then you'd be better off posting the unformatted code, your question is incomplete without it – Sayse Jan 12 '21 at 22:18
  • @JohnD: I know how 'None' got printed, I want to know how the function executed and printed the value 4 in the first place. – harry Jan 12 '21 at 22:20
  • 1
    Because you put `square(2)`, which _is a function call_, as an argument to `print()`. – John Gordon Jan 12 '21 at 22:22

2 Answers2

2

Presumably you have something like this:

>>> def square(n):
...   print(n ** 2)
... 
>>> print(square(2))
4
None

You don't have a return statement so python returns None. Try this instead:

>>> def square(n):
...   return n ** 2
... 
>>> print(square(2))
4

The reason why the 4 got printed is becuase you called square(). Calling square had the side-effect of printing the square since that's what it's definition body says to do.

Coder
  • 1,175
  • 1
  • 12
  • 32
  • "'None' I can understand, but why did Python execute the function and output 4 here?" - Your answer is the opposite of what the op is asking about. Its better to wait for enough details than to presume – Sayse Jan 12 '21 at 22:15
  • 1
    I understood why Python gave me 'None'; i wanted to know why it still executed the square() function when all I had done was write a print statement. Your code is exactly what I meant, btw. – harry Jan 12 '21 at 22:16
  • @Sayse it was pretty clear what he meant, as he said above my code is exactly what he meant. I will edit it with why it still executed the function so you can remove the downvote :) – Coder Jan 12 '21 at 22:17
  • So even if a function is called inside of a print statement, it still executes as normal? – harry Jan 12 '21 at 22:22
  • 1
    yep since you are calling it. If you wanted to just print the mem address without calling it, you can do `print(square)` which will give something like `` – Coder Jan 12 '21 at 22:23
  • 1
    That's what I needed to know, thanks. can you elaborate on the memory address thing? – harry Jan 12 '21 at 22:24
  • 2
    To _call_ a function, you write its name and put `()` after, like so: `square(5)`. However if you leave off the parentheses, then you have the _function object_, which is fundamentally just an object like any other, and can be printed just like any other object. The default representation for function objects is to display the function name and memory address. – John Gordon Jan 12 '21 at 22:36
0

By placing the () next to the function, you called it. So it got executed. The print statement around it caused the return value None to be printed, while inside it printed the square.

If you don't want to call the function, remove the () (and any parameters).

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
  • 1
    I didn't downvote, but just this: do functions execute even when they're called from inside print statements? – harry Jan 12 '21 at 22:23
  • 1
    @HarryHolmes : Yes, Python is made in a way that you can use the return statements from functions to print something, like `print(getUserId("this is my userName"))` It looks better than: `userId=getUserId("this is my username"); print(userId)`, so they made it that way. – Lakshya Raj Jan 12 '21 at 22:24