DOC PREVIEW
TAMU CSCE 110 - Functions (cont.)
Type Lecture Note
Pages 5

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 8Outline of Last Lecture:I. FunctionsA. ArgumentsB. Pass/ReturnC. MainII. Using functions to write a programOutline of Current Lecture:I. More on FunctionsII. Function VariablesA. Global VariablesB. Local VariablesIII. Built-In FunctionsIV. ListsCurrent Lecture:I. More on Functions It is useful to write several variations of programs to help you understand the execution of the program and how different commands can change the entire output.Let's just start by looking at different examples of programs that use user-defined functions.1 >>> def f1(x):2 >>> return x + 5 3 >>> print "hello"4 >>> def main():5 >>> number = int(raw_input("Enter a number: "))6 >>> print f1(number)7 >>> main()We have two user-defined functions: f1 and main. The order of line execution is: 7, 4, 5, 6, 1, 2, 6. Note that line 6 is visited twice: once to see which function it's calling, and once to actually print the value that the function returns. Remember that the program does not look at the definitions of functions until they are called, which is why we start at line 7. Notice that line 3 is never executed, even though the function f1 is called. This is because of the "return" in line 2. As soon as a value is provided to return back toline 6, the function ceases to play a role in the program. We could put anything in line 3 and it would be a useless statement. We can also put other types of statements within functions, such as if/else statements:These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.>>> def f1(x):>>> if x > 10:>>> return x + 5>>> else:>>> return x>>> def main():>>> number = int(raw_input("Enter a number: "))>>> y = f1(number)>>> print y>>> main()Notice that we can call functions in ways other than "print." We can set it equal to variables andprint the variables if we chose to.II. Function VariablesA. Global VariablesVariables are either local or global. A global variable is a variable that is outside of a function and applies to the program as a whole. Here, z is an example of a global variable:1 >>> z = 102 >>> def f1(x):3 >>> if x > 10:4 >>> return x + 55 >>> else:6 >>> return x7 >>> def main():8 >>> number = int(raw_input("Enter a number: "))9 >>> y = f1(number)10 >>> print y11 >>> print z12 >>> main()Let's say the user inputs number = 2. The line execution order for this program is 1, 12, 7, 8, 9, 2,3, 5, 6, 9, 10, 11. The output for this program is:>>> 2>>> 10B. Local VariablesLocal variables are located within functions and only apply to the function they are within. Looking at the local variables in the program directly above, x is a variable local to f1, and y is a variable local to main. The variable z, though called within main, is still a global variable. If a variable is called in a function and is not found within the definition of that function, the program will then look outside of the function to check and see if it is a global variable. 1 >>> z = 102 >>> a = 673 >>> def main():4 >>> a = a + 235 >>> print "z", z6 >>> print "a", a7 >>> main ()This program will get an error. It is the variable a that causes the error. In line 4, we are assigninga value to a, while at the same time we are referencing a previous value of a. Because a is local to the function main, the program will not look outside the function to see the global variable a = 67. So, within the function, a is referenced before it is given an assigned value. III. Built-In FunctionsBuilt-in functions are functions that are already defined within Python. We have already used some, such as print, int(), raw_input(), str(), and float(). Here are some others:min() calculates the minimum value in a given list of valuesmax() calculates the maximum value in a given list of valueslen() calculates the length of a string (number of characters)sum() calculates the sum of all entries in a given listsorted() sorts the entries in a given list, can be numerically or alphabeticallyrange() creates a list of all items between two given pointsHere are some basic examples of input/output:>>> min(10, 839, 93, -10)>>> -10>>> len('python')>>> 6>>> max('string', 'hello', 'hi')>>> 'string'This chooses the entry that comes last alphabetically. The min() function does the opposite.>>> max('hello', 'hello')>>> helloThis is trivial, as both entries are the same.IV. ListsLists give a lot of flexibility when writing programs. Lists are groups of individual entries, which can be of any type. We denote lists using brackets. >>> y = ["apple", 12, 14.1, "python"]Finding the index of a list is similar to finding the index of a string. The difference is that the index of a list is an entire entry, not just one character. For example:>>> print y[1]>>> 12>>> y[1:3]>>> [12, 14.1]>>> y[0]>>> "apple"We can calculate individual characters/indexes of entries in the list as well.>>> y[0][0]>>> "a">>> y[0][-1]>>> "e"We can also have lists within lists. z = [10, 20, [30, 40, 50, 60], 70]Even though z[2] is a list, it is still one individual entry.>>> z[2]>>> [30, 40, 50, 60]>>> z[2][1]>>> 40Here are some examples of input/output using lists:>>> a = [10, 30, 40, 20, 5]>>> sorted(a)>>> [5, 10, 20, 30, 40]>>> a*2>>> [10, 30, 40 20, 5, 10, 30, 40, 20, 5]This copies the list and merges it all into one. >>> sum(a)>>> 105>>> range(0, 10)>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]This creates a list that includes all items between the start and just before the end point.>>> m = [1, 2, 3]>>> n = [5]>>> m + n>>> [ 1, 2, 3, 5]One very important differentiation between lists and strings is that strings are immutable, while lists are mutable. In other words, we can change an entry/the contents of a list but not of a string. It is similar to assigning new values to variables. For example:>>> e = [1, 2, "yellow", "blue"]>>> e[1] = "red">>> e>>> [1, "red", "yellow", "blue"]We can also create a program that automatically changes/adds entries of list. >>> import random>>> new_list = []>>> while i < 10:>>> new_list += [random.randint(0,100)]>>> i += 1>>> print


View Full Document

TAMU CSCE 110 - Functions (cont.)

Type: Lecture Note
Pages: 5
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Functions (cont.)
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions (cont.) and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions (cont.) 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?