Python Functions

A function is a block of code the runs only when the function is called.

Example

Python
def myFunction():
    print('Running myFunction()')

myFunction()

Output

Running myFunction()

Functions can take parameters and return values.  Returning a value uses the keyword return.

Example

Python
def add(a, b):
    sum = a + b
    return sum

mysum = add(1, 2)
print(mysum)

Output

3

Notes

The above code example is more of a "step-by-step" example and could be reduced to:

Python
def add(a, b):
    return a + b

print(add(1, 2))

Functions cannot be empty, but sometimes you want to create a placeholder for a function.  To do this, use the special keyword pass.

Example

Python
def implementLater():
    pass