Functions without parameters

The mission link:https://app.dataquest.io/m/316/functions%3A-intermediate/8/functions-code-running-quirks

I’ve understood that functions without parameters don’t accept arguments, am I right?

And here, how the type of x is NoneType although 3.14 is assigned to x?
Screenshot 2021-01-03 051826

And if I use return instead of print, the type of j will be float, am I right?

Functions without a return statement don’t return any value. However, strictly speaking, they return a None value, which practically represents the absence of a value. The None value is an instance of the NoneType data type

Since it doesn’t return any value it returns None. So yes, you are right.
If you add a return statement instead of print(), it will return float. So yes, you are right again.

I hope you have already tried and figured it out by yourself.

1 Like

Yes, I’ve tried but I wanted to make sure I am right.
Thanks a lot.

And here, how the type of x is NoneType although 3.14 is assigned to x?
And why the output of j=print_constant() is 3.14 and the output of print(j) is None?
Screenshot 2021-01-03 051826

Hi @Maho

Have a look again at your code. Are you finding the type of x?
What is assigned to j?
This might help you find the answer by yourself.

If that didn’t help, here is the explanation

Since your function doesn’t have a return type, it will return None which you already know. So when you assign print_constant() to the variable j, the function gets executed and store None value to the variable j. But since the function has a print statement inside of it, it will print whatever it is supposed to print. But the value it stores and returns is None.

So the first line you see on the output 3.14 is the result of print statement and the second line None is the actual value stored in j.

When you get the type of j it obviously returns None type.

You can also see that the variable x defined inside the function doens’t really come in the picture. I hope this gives you a better idea.

1 Like

By default, a function without an explicit return statement returns None.
Therefore, print_constant return None.

def print_constant():
      x = 3.14
     print(x)

x is a local variable and the scope of x is within the function print_constant.

There is no global assignment of any variables. The function print_constant simply prints 3.14 and returns None.

print_constant function can be simplified to the following:

def print_constant():
    print(3.14)

x can be eliminated since we can substitute x with whatever x is assigned to.

Usually, we defined constants with all capitalize variable because capitalize variable name is understood to be constant.

def print_constant():
    X = 3.14
    print(X)

We can do one better and rename X to pi to what it exactly means.

def print_constant():
    pi = 3.14 
    print(pi)
1 Like

Thanks a lot, @jithins123

1 Like

Thanks a lot, @alvinctk