3. Functions
6. Fruitful functions
13. Case study: data structure selection
B. Analysis of Algorithms
A function is a name for some lines of code.
The top line of a function is named a header. If something is in the parentheses in a header it is called a parameter.
The line of code that invokes a function is named a call. If something is in the parentheses in a call it is called an argument.
With positional passing the order of the arguments determines the initial parameters' values.
The None keyword is used to define a null value, or no value at all.
It is usually best to just do processing in a function so it is easily resusable in situations where the input and output will be different.
Do not create functions inside of other functions. All functions should be on the left margin.
You can put a function definition in another file. The file that calls the function must do import fileName and then call with fileName.functionName or you can do from fileName import functionName and then you can just call with functionName.
The function definition must be above a call to it.
In all languages, functions should be descriptive and include an verb
In Python, the convention is to name functions in all lowercase with words separated by underscores
# Prof. Vanselow
# An integration of everything I have learned about programming
import OutputDemo
def main():
print("Welcome to my Integration Project!")
continue_program = True
while continue_program :
print("Enter the choice for what you would like to see")
print("1. Area calculator")
print("2. Output formatting")
print("5. Quit")
user_choice = int(input())
if user_choice == 1:
side = int(input("Enter the length of the side of a square to get the area:"))
print(calculate_area(side))
elif user_choice == 2:
OutputDemo.do_output_format_demo()
elif user_choice == 5:
continue_program = False
else:
print("Invalid selection. Try again.")
def calculate_area(side):
return side * side
main()