Download presentation
Presentation is loading. Please wait.
Published byJulian Amos Melton Modified over 8 years ago
1
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the lab machines Midterm 1 October 6 th 2011 WTHR 200 Midterm 2 November 8 th 2011 WTHR 200
2
Project 1 You will be tasked with writing function to calculate: the velocity need to hit a pig at a given distance and elevation with a certain angle.
3
Project 1 You will be tasked with writing function to verify the trajectory using the information you calculated in the previous function. Did rounding affect the calculation?
4
Project 1 You will be tasked with displaying some of the information you calculate graphically and “gluing” the various parts together in one larger “main” function.
5
Python Boot Camp! Multiple Arguments Returns Passing a value out of a function (output) Last statement executed Scope Local Variables Comments / Coding Practices
6
Indentation Indentation is very important in python In functions it defines where the function ends Python will give errors if the indentation is incorrect
7
Multiple Arguments Functions can take multiple arguments def sumOfTwo(a,b): return a+b def sumOfThree(a,b,c): return a+b+c def sumOfFour(a,b,c,d): return a+b+c+d
8
Call Sites You must provide the number of arguments the function expects def sumOfTwo(a,b): return a+b print(sumOfTwo(4,5)) def sumOfThree(a,b,c): return a+b+c print(sumOfThree(3,3,3)) def sumOfFour(a,b,c,d): return a+b+c+d print(sumOfFour(3,3,3)) ERROR!
9
Clicker Question: Are these programs equivalent? def myFun(a): print (a) return 5 print(myFun(4)) def myFun(a): print (a) return 5 res = myFun(4) print(res) 21 A: yes B: no
10
“Capturing” the return The return statement defines the value that is passed out from the function We can store the return value in a variable def sumOfTwo(a,b): return a+b result = sumOfTwo(4,5)) def sumOfThree(a,b,c): return a+b+c result = sumOfThree(3,3,3)
11
Stringing together functions We can build up complex expressions from functions def sumOfTwo(a,b): return a+b print(sumOfTwo(sumOfTwo(4,5), sumOfTwo(1,2))) OR a = sumOfTwo(4,5) b = sumOfTwo(1,2) c = sumOfTwo(a,b) print(c)
12
A “Sample” Program def task1(a,b): … y = …. return y def task2(a,b,c): …. z = … return z def main(): input1, input2 = eval(input(…)) output1 = task1(input1, input2) output2 = task2(input1, input2, output1) print(output2)
13
More on Returns Returns are the last statement executed by a function The function is considered to be “done” at this point There may be other statements “after” the return, but these are omitted def sumOfTwo(a,b): return a+b print(a+b) print(sumOfTwo(4,5))
14
Clicker Question: Are these programs equivalent? a = 3 def myFun(a): print (a) myFun(4) a = 3 print (a) 21 A: yes B: no
15
Naming and Binding Arguments are bound at the “call site” Call site: a point in the program where the function is called The binding is active up until the return of the function
16
Naming and Binding a = 3 def myFun(a): print (a) myFun(4) print(a)
17
Local Variables We saw that arguments can be rebound for the duration of the call What about variables that we define in the function body?
18
Local Variables Abstractly there are two types of local variables 1) those which introduce a new variable 2) those which redefine an existing variable which was initially defined outside the function definition Local variables, just like arguments, have a lifetime associated with the function call
19
Local Variables a = 3 y = 10 def myFun(a): print (a) y = 1 myFun(4) print(a) print(y)
20
Clicker Question: does this program print 3 or 4? x = 3 def myFun(): print (x) x = 4 myFun() A: 3 B: 4
21
Homework Work on Project 1 Read chapter 6 (functions)
22
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the lab machines PreLab 4 Important! Practice Lab 4 Graphics Library methods
23
Python Boot Camp! Local vs Global Variables Comments / Coding Practices
24
Variables and Functions Variables used in functions but defined outside of the function can be changed Multiple calls to a function may yield different results if the program “rebinds” such variables
25
Variables and Functions x = 3 def myFun(): print (x) x = 4 myFun() x =5 myFun()
26
You must be careful! x = 3 def myFun(): print (x) x =1 x = 4 myFun() x =5 myFun() ERROR!
27
Global Variables How can we get the example code we saw earlier to work? Python is not sure if we want x to be a local variable or if it should refer to the x we defined outside of the function We can inform python if we want x to refer to the variable outside of the function New keyword global
28
This works! x = 3 def myFun(): global x print (x) x =1 x = 4 myFun()
29
Global or local? If the global keyword is use, the variable is global If the first use of the variable is a read the variable is global NOTE: We cannot assign to the variable The variable is not defined in the arguments If the first use of the variable is a write (assignment) the variable is local Unless the variable is defined global
30
Clicker Question: Is X global or local? x = 3 def myFun(): y = 4 z = x + y myFun() A: global B: local
31
Lets Review Functions take input and produce output Output is provided by the “return” statement Otherwise the function does not provide output At the call site of the function the arguments get bound The arguments can rebind variables that have already been defined for the duration of the call You can use variables defined outside the function but you must be careful!
32
Function Arguments Functions typically assume something important about the arguments Will this work no matter what we provide as arguments? def sumOfTwo(a,b): return a+b
33
Function Arguments Consider the following three cases: One of these cases will throw an error. This behavior is defined by the code inside the function res = sumOfTwo(1,2) res = sumOfTwo(“Hello “, “World”) res = sumOfTwo(“Hello”, 1)
34
Function Arguments There are two ways to handle this difficulty 1. Tell everyone what the function expects 2. Include checks inside the function to ensure the arguments are what is expected A combination of both techniques should be used
35
Function Documentation This solution uses comments and if-statements. We will revisit this in later slides #This function expects two integers and returns -1 def sumOfTwo(a,b): if (type(a) == int and type(b) == int) : return a+b return -1
36
Functions that Modify Parameters Suppose you are writing a program that manages bank accounts. One function we would need to do is to accumulate interest on the account. Let’s look at a first- cut at the function. def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance
37
Functions that Modify Parameters The intent is to set the balance of the account to a new value that includes the interest amount. Let’s write a main program to test this: def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount)
38
Functions that Modify Parameters We hope that that the 5% will be added to the amount, returning 1050. >>> test() 1000 What went wrong? Nothing!
39
Functions that Modify Parameters The first two lines of the test function create two local variables called amount and rate which are given the initial values of 1000 and 0.05, respectively. def addInterest(balance, rate): newBal = balance*(1+rate) balance = newBal def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount)
40
Functions that Modify Parameters
42
def addInterest(balance, rate): newBalance = balance * (1 + rate) return newBalance def test(): amount = 1000 rate = 0.05 amount = addInterest(amount, rate) print(amount) test()
43
Functions that Modify Parameters amount = 1000 rate = 0.05 def addInterest(): global amount global rate amount = amount * (1 + rate) addInterest() print(amount)
44
What all can I do within the body of a function? Define local variables Call other functions Define new functions
45
Homework Work on Project 1 Do the pre lab / review chapter 4 Read Chapter 6 Read Python Wiki “Functions” http://en.wikibooks.org/wiki/Python_Programming/Funct ions http://en.wikibooks.org/wiki/Python_Programming/Funct ions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.