-1

If I declare a function and pass a value to that function that is working fine

def aFunction(num):
    print("This is a function", num)
print(aFunction(100))

Output

This is a function 100

If I declare another function inside that function and pass the same value it prints None.

def aFunction(num):
    print("This is a function", num)

    def anotherFunction(num):
        print("This is another function", num)

print(aFunction(100))

Output

This is a function 100
None

The value I'm passing is not accessible by the nested function. Why this is happening?

Roshin Raphel
  • 2,478
  • 3
  • 18
  • 34
MONTASIM
  • 316
  • 2
  • 8
  • 2
    your first example also prints `None` at the end... the reason is the you don't return anything, so your function returns `None` by default, and you print it – Adam.Er8 Jul 22 '20 at 08:03

1 Answers1

2

You have only declared the inner function, not called it. You have to call the nested function as below to get the function working :

def aFunction(num):
    print("This is a function", num)

    def anotherFunction(num):
        print("This is another function", num)
    anotherFunction(num)
aFunction(100)

Additionally, you don't need print(aFunction(100)) only the function call, aFunction(100) is required. This is because you have not specified any return values for the function, so by default, it returns None and the print function catches it and print None.

Roshin Raphel
  • 2,478
  • 3
  • 18
  • 34