CSC111 Exercises with Value Returning Functions

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 19:34, 4 March 2014 (EST)



Exercise 1

  • Predict the output of this program but analyzing it and figuring out its execution:
def Dave( a ):
    return a * 2

def Carl( a, b ):
    sum = a + b 
    a = 0
    b = -1
    return sum

def main():
    x = 3
    y = 5
    print( Dave( x ) )
    print( Dave( y+1 ) )
    print( Carl( x, y ) )
    print( Carl( 0, 0 ) )
    print( Carl( Dave( x ), Dave( y ) ) )
    print( Dave( Carl( x, 0 ), Carl( 0, y ) ) )

main()


Exercise 2


Write a function that receives 1 parameter and returns True if the parameter is even, and False otherwise. Write a small program that uses the function.

Exercise 3


Redo the Rock, Paper, Scissors program using functions. Use a top-down approach.