# Chapter 9: Variable scope and functions¶

### Example 9.1, Page number: 160¶

In [1]:
def func() :
if not hasattr(func, "permanent") :
func.permanent = 1
result = func.permanent
func.permanent += 1
return result

for counter in range (0, 3) :
temporary = 1
print ('Temporary %d Permanent %d' % (temporary, func()))
temporary += 1

Temporary 1 Permanent 1
Temporary 1 Permanent 2
Temporary 1 Permanent 3


### Example 9.3, Page number: 164¶

In [2]:
def triangle (width, height) :
area = width * height / 2.0
return area

print ('Triangle #1 %f' % (triangle (1.3, 8.3)))
print ('Triangle #2 %f' % (triangle (4.8, 9.8)))
print ('Triangle #3 %f' % (triangle (1.2, 2.0)))

Triangle #1 5.395000
Triangle #2 23.520000
Triangle #3 1.200000


### Example 9.4, Page number: 166¶

In [4]:
def length (string) :
return len(string)

line = 'hello world'

print ('Length is: %d' % length(line))

Length is: 11


### Example 9.6, Page number: 170¶

In [5]:
def length (string) :
return len(string)

line = 'Steve Oualline'

print ('Length is: %d' % length(line))

Length is: 14