- Used to package code statements so that they can be re-used
- First form abstraction.
- Specifically, functional abstraction.
- Basic parts:
- Header: begins with a keyword and ends with a colon ":"
- Body: one or more Python statements, each indented the same amout
- Function definition
- Function call
- Return Value
- Functions can call other functions.
- You often need to describe what a variable is used for or what a function does in plain English
- Almost all programming languages allow comments.
- In Python, you start a comment with the "#" symbol:
# This is a comment
- We will start to use more comments in our code.
- Comments are useful for you and for other people looking at your code.
# Exmple of a simple function with no parameters and no return value
# Example of a function with parameters
# Example of a function with parameters and a return value
- A way to let your program know about other functions:
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
- Import is extremely useful for letting your Python programs use existing functions.
Flow of Execution
- Execution of Python programs start on the first line and continue one by one
- However, when a function is called, the flow jumps to the function
- The arguments are assigned to the parameters
- The body is executed (statement by statement)
- Then the function returns to the caller
- Functions can call functions.
- This is little bit like web-browsing and using the "back" button.
Variables, Parameters, and Stack Diagrams
- The parameters of a function become local variables for the duration of the function execution
- A function can create new local variables.
- The local variables "go away" when the function returns
- A stack diagram can be used to follow the flow of execution and the values of variables.
- All web pages are encoding in a special language called HTML (HyperText Markup Language)
- Browsers understand how to interpret HTML and render it to your screen
- You can "view source" to see the HTML for any web page
- Basic structure:
- You can put HTML files in your CS "web" directory
- Your URL (Universal Resource Locator) is
- Give file proper permissions: