Example 1
def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a )
singthrice( x ) x def singthrice( x ): sing the value in x three times, loudly 2 def hop( y ): hop y times main() def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a )
def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) hop( y ) y 1
singthrice( x ) x def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times 154 main() def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a )
def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) -1
singthrice( x ) x def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) -1
def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) -1 hop( y ) y 2
def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) 1
def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) 1 hop( y ) y
Notes on Example 1 literals as arguments: 2, 154 variables as arguments: a pass by value: argument to parameter main's local variables: a singThrice's local variables: x hop's local variables: y scope of local variables: only understood in function in which they are defined.
Example 2
def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S')
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c " T" def writeThisLetter( c ): write the value of c main() def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') output
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c " T" def writeThisLetter( c ): write the value of c return "U" main() def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') output
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c " T" def writeThisLetter( c ): write the value of c return "U" main() x def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" output
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c "R" main() x def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" output
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c "R" main() x def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" return "S" output
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c "R" main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" return "S" "S" output
def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" "S" output GO SU!
def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" "S" output GO SU! USA
def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" writeThisLetter(c) c "S" "B" output GO SU! USA
def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" writeThisLetter(c) c "S" output GO SU! USA "U"
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y "G" def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" "S" output GO SU! USA
tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y "G" def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" return "H" "S" output GO SU! USA
def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+" !") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" writeThisLetter(c) c "S" output GO SU! USA "S"
Notes on Example 2 string literals as arguments : 'T' or "T" return values may be stored in a variable and used: x=tellNextLetter('T') or return values may be ignored: tellNextLetter('G') scope of parameters