Difference between revisions of "CSC111 surprising behavior"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <source lang="python"> # What is the surprising behavior # of this program? def f1( L ): L.sort() print( "smallest element of L = ", L[0] ) print( "larg...")
 
 
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 09:58, 1 November 2011 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] 09:58, 1 November 2011 (EDT)
 
----
 
----
 +
==First, a program with the "normal" behavior==
 +
 +
<br />
 +
<br />
 +
<source lang="python">
 +
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()
 +
 +
</source>
 +
<br />
 +
<br />
 +
==Next, something behaving differently...==
 +
<br />
 +
<br />
 
<source lang="python">
 
<source lang="python">
 
# What is the surprising behavior
 
# What is the surprising behavior

Latest revision as of 08:36, 3 November 2011

--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()