


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Professor: Wolber; Class: Intro to Computer Science I; Subject: Computer Science; University: University of San Francisco (CA); Term: Spring 2008;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
A function is a named sequence of statements that performs a desired operation. When you brush your teeth in the morning, you
In an earlier tutorial, we called some built-in functions of Python, such as ‘type’ and ‘len’ The 'len' function returns the length of a list: while i<len(list1)
Functions in programming are like math functions: you provide the name of the function, e.g., 'len', followed by an open parenthesis, and then some parameters (e.g., list1) and then a closed parenthesis. The function returns a value to the caller. In the example above, the return value is part of a condition. Other times, you'll want to "catch" the return value in a variable, e.g., length = len(list1) Built-in functions like 'len' can be called from any python program or the interpreter. But there are also thousands of Python functions that are not 'built-in' but are still quite useful. There are many Python libraries to use, and you can also create libraries of your own.
Within the interactive interpreter or a program file, you can import existing python code. We call collections of reusable code a 'code library' or a 'module'. For Mastermind, you’ll need the ‘random’ module. For this tutorial, we'll import the math module. From the interactive interpreter prompt, type the following:
import math math.sqrt(16) The import statement says you want to use math functions. But when you import a module, like 'math', you must qualify any function calls you make to functions with "math." For the above example, if you typed in just 'sqrt(16)' you'd get an error. 'math.sqrt(16)' is understandable to Python because it knows that sqrt is within the math module. Try the following math functions pow (x,y) -- returns x to the yth power, so pow(2,3) is 8 acos(x) – returns cosine of x
The calling code often catches the return value in a variable, e.g., val = square(2) or list=[3,5,2] total = totalList(list) But as you saw with the 'len' function, sometimes the function call is part of another construct, like a condition.
With functions, one can break a program into simple parts. A typical Python program will have several functions and a 'main' body of code, as below: def func1(param1,param2): #... def func2(param1): #... #... more functions
#...