Functions
A function is a set of code that can be called by name. You can write your own, or just call a pre-existing function.
The print function contains all necessary code to tell the computer to print to the screen.
The round function contains all necessary code to tell the computer to round a floating-point number to a specified decimal length (3.1448 rounds to 3.14)
Best Practices: The name of your function should describe the function. I.E. "getAge" to output age, "setTime" to to set the
time, or "calcAreaSquare" to calculate the area of a square.
To write your own function:
def <name of function>(parameter 1, parameter 2):
<statement(s)>
Examples of Creating Functions:
def calcAge(birthYear, year):
print("You are",yearbirthYear,"years old.")
birthYear = int(input("What is your birthyear? "))
year = int(input("What is the current year? "))
calcAge(birthYear, year)
List of Functions - A list of functions with brief descriptions.
Modules
A module is a group of functions, usually with a common theme. The __builtin__ module is always available and contains functions like abs and round. Other modules, such as math, can be imported.
Import a module:
To import a module with all of it's functions:
import <module name>
To import specific functions from a module:
from <module name> import <function1>, <function2>, <etc>
The main file of a program is called the "Main Module" and can be imported like any other module:
import myprogram.py