Download presentation
Presentation is loading. Please wait.
1
Example 1
2
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 ) # #not a a a hop( 2 ) a = 1 hop( a )
3
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 ) # #not a a a hop( 2 ) a = 1 hop( a )
4
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 ) # #not a a a hop( 2 ) a = 1 hop( a ) hop( y ) y 1
5
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 ) # #not a a a hop( 2 ) a = 1 hop( a )
6
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 ) # #not a a a hop( 2 ) a = 1 hop( a ) -1
7
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 ) # #not a a a hop( 2 ) a = 1 hop( a ) -1
8
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 ) # #not a a a hop( 2 ) a = 1 hop( a ) -1 hop( y ) y 2
9
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 ) # #not a a a hop( 2 ) a = 1 hop( a ) 1
10
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 ) # #not a a a hop( 2 ) a = 1 hop( a ) 1 hop( y ) y
11
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.
12
Example 2
13
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')
14
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
15
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
16
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
17
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
18
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
19
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
20
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!
21
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
22
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
23
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"
24
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
25
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
26
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"
27
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.