Python Functions, Packing, and Unpacking Getting Info In and Out of Functions Arguments and Parameters

why second output is NONE? def add_two(num): val = num + 2 print(val) print (add_two(10))

tell me please why first output is 10(right) and second is NONE?

def add_two(num): val = num + 2 print(val)

print (add_two(10))

def add_two(num): val = num + 2 print(val)

print (add_two(10))

RESULT IS...

treehouse:~/workspace$ python functions.py
12
None

why None???

6 Answers

Josh Keenan
Josh Keenan
17,734 Points

Can you tell me what the second input is?

print(add_two(10))

Please tell me what you passed in as an argument for add_two() when it outputted 'None'.

print(add_two(10))

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,187 Points

When a function doesn't have a return statement the default return value is None. The following will print 12 in both cases.

def add_two(num):
  val = num + 2
  print(val)
  return val

print(add_two(10))
KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,187 Points

This has already been answered. Your add_two() function returns None because it has no explicit return statement. As a result print(add_two(10)) displays None.

thanks guys!!

Ary de Oliveira
Ary de Oliveira
28,160 Points
# ====> in global scope.
# ====> When a function doesn't have a return
# statement the default return value is None.
def print_name():
    print('Ary de Oliveira')

def print_favorite_name():
    print('Ary')

numero = 120

def set_numero():
    numero = 60
    key = 'a'
    print(key)

print(set_numero())
print(numero)
print_name()
print_favorite_name()

my_var = 10
def multiply():
    my_var = 10 * 2
print(my_var)
#ary