Lectures‎ > ‎



  • 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
  • Parameters
  • 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.

Function Examples

# Exmple of a simple function with no parameters and no return value
def newline():
# Example of a function with parameters
def cat_n_times(s, n):
    print s * n

# Example of a function with parameters and a return value
def avg(n1, n2):
    avg = (n1 + n2) / 2.0
    return avg

# Print 3 newlines:

# Print _USF 4 times
cat_n_times('_USF', 4)

# Compute averages
a1 = avg(20, 21)
print 'a1 = ', a1

a2 = avg(100, 200)
print 'a2 = ' + str(a2)


  • A way to let your program know about other functions:
$ python
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.
>>> from funcs import *

a1 =  20.5
a2 = 150.0
>>> print avg(1,2)
  • 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
  • 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:
Hello World from HTML!

  • Lists:

  • You can put HTML files in your CS "web" directory
    • /home/web/<username>
  • Your URL (Universal Resource Locator) is
    • http://www.cs.usfca.edu/~<username>
  •  Give file proper permissions:
    • chmod 644 <filename> 
Greg Benson,
Sep 4, 2012, 4:26 PM
Greg Benson,
Sep 4, 2012, 4:26 PM
Greg Benson,
Sep 4, 2012, 4:26 PM
Greg Benson,
Sep 4, 2012, 4:26 PM
Greg Benson,
Sep 4, 2012, 4:26 PM
Greg Benson,
Sep 4, 2012, 4:26 PM