CSC111 surprising behavior

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 09:58, 1 November 2011 (EDT)


First, a program with the "normal" behavior



def f1( a ):
    print( "in f1(): a = ", a )
    a = a * 2
    print( "in f1(): a = ", a )

def main():
    x = 10
    print( "x = ", x )
    f1( x )
    print( "x = ", x )

main()



Next, something behaving differently...



# What is the surprising behavior
# of this program?


def f1( L ):
    L.sort()
    print( "smallest element of L = ", L[0] )
    print( "largest element of L = ", L[-1] )


def main():
    L = [ 1, 10, 3, 0, -10, 100, 2 ]
    print( "L = ", L )

    f1( L )

    print( "L = ", L )

main()