Error in define function:

Screen Link:

My Code:

First case: 

def welcome(a_string):    
#    return a_string+ 50
    print('Wellcome to ' + a_string + ' !')

dq = 'dataquest'
py = 'Python'
jn= 'Jupyter Notebook'

print(welcome(dq))

What I expected to happen:

I expected to see the only line: Wellcome to dataquest !

What actually happened:

Actually, I see 

Wellcome to dataquest !
None

Can any body please explain why I see “None” ?

Second case:
Another basic question: why the code is working even if I haven’t mentioned the ‘return’ ?

Say I wanted to sum a_string+ 50 if I don’t put return a_string+ 50 it is showing the error. In that case I called welcome(40).

If I do return a_string+ 50

and print(welcome(40))
it is giving me 90.

So why it is working for 1 st case not for the 2 nd case. I expected same answer.

1 Like

@mondalsou: In the first case you are calling the function with welcome(dq). Then you attempt to print it internally in the function, resulting in string Wellcome to dataquest ! .

Thereafter the function automatically returns with the default value of None (similar to null in other programming languages and signifying the absence of a value). The value returned to the function is then passed on to the built-in function print(), which regurgitates None.

So the output is as follows:

Wellcome to dataquest !
None

By default python unlike other languages has sort of a “fallback” if you don’t specify it and the default return value is None.

There is no error in this case… if you would kindly elaborate as your description is not very clear. A code snippet would be helpful.

As shown above, None is returned.

2 Likes

I got your point. Now, I understand what I was doing wrong:

I found that both are giving the same results:-

Screenshot 2021-07-15 at 4.48.34 PM

Screenshot 2021-07-15 at 4.49.53 PM

Thanks

2 Likes