Table of Contents

Functions

Functions

Code Example

def main():
    x = 5
    y = 12
    z = add(x, y)
    print(z)    

def add(a, b):
    c = a + b
    return c

if __name__ == "__main__":
    main()

So you see, you can use the add() function to add numbers.

Homework

Advanced

Is this too easy? Here are some more challenging questions:

or maybe,

Example answer!

def isEven(a):
    x = a // 2      #NOTE: / always returns a float. // always returns the lowest integer!
    if a == x:
        return "even"
    else:
        return "odd"

There are many, many ways to do this. The students should be able to think of at least this:

Example answer 2!

def isEven(a):
    while a >= 2:
        a = a - 2
        
    if a == 0:
        print("even")
    else:
        print("odd")

If this challenge is too easy, they can always write a function that determines if a number is prime.