Computer Studies‎ > ‎Python‎ > ‎Python Tutorial‎ > ‎

Functions

Observe the following code snippet:

# define function
def aToB(n1,n2):
    return n1**n2

# variable assignments
a = 2
b = 3

# print the power, using the function
print aToB(a,b)

In this program, a function named aToB() is declared using the def statement. The function accepts two arguments n1 and n2, and returns the value n1**n2.

To use the function, you simply call the function name with the appropriate arguments. If there are no arguments, the parentheses are left blank (but they are still present). 

Note that functions must appear in the program before they are called. You may wish to group all functions together near the beginning of your programs.

Assignment 1

Recalling the math menu program challenge A-1 in this series of challenges, rewrite this program so that each menu choice calls an appropriately-named function.
Comments