Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015.

Similar presentations


Presentation on theme: "Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015."— Presentation transcript:

1 Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

2 Xiaojuan Cai Computational Thinking 2 Objective To understand why using functions. To be able to define new functions. To understand the details of function calls and parameter passing. To write programs that use functions to reduce code duplication and increase program modularity.

3 Xiaojuan Cai Computational Thinking 3 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

4 Xiaojuan Cai Computational Thinking 4 What is function? A function is like a subprogram, a small program inside of a program. The basic idea: write a sequence of statements give that sequence a name (definition) execute this sequence at any time by the name (function call).

5 Xiaojuan Cai Computational Thinking 5 Functions so far Our programs comprise a single function called main(). Built-in Python functions ( abs ) Functions from the standard libraries ( math.sqrt ) Functions from the graphics module ( p.getX() )

6 Xiaojuan Cai Computational Thinking 6 Motivating example Happy Birthday lyrics … def main(): print "Happy birthday to you!" print "Happy birthday to you!" print "Happy birthday, dear Fred..." print "Happy birthday to you!" Remove duplicates: def happy(): print "Happy birthday to you!“

7 Xiaojuan Cai Computational Thinking 7 Motivating example Using parameters def sing(person): happy() happy() print "Happy birthday, dear", person + ".“ happy() A paramater is a variable that is initialized when the function is called.

8 Xiaojuan Cai Computational Thinking 8 The function of functions Having similar or identical code in more than one place has some drawbacks: writing the same code twice or more. this same code must be maintained in two separate places. Functions can be used to reduce code duplication and make programs more easily understood and maintained.

9 Xiaojuan Cai Computational Thinking 9 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

10 Xiaojuan Cai Computational Thinking 10 Functions, formally A function definition looks like this: def ( ): The name of the function must be an identifier Formal-parameters is a possibly empty list of variable names. def func(x): y = x * x return y a = func(2)

11 Xiaojuan Cai Computational Thinking 11 Function calls ( ) Four-step process: The calling program suspends at the point of the call. The formal parameters of the function get assigned (by position). The body of the function is executed. Control returns to the suspended point.

12 Xiaojuan Cai Computational Thinking 12 Function calls illustration

13 Xiaojuan Cai Computational Thinking 13 Future value with a function Duplicated codes for drawing bars. To properly draw the bars, we need three pieces of information. The year the bar is for How tall the bar should be The window the bar will be drawn in These three values can be supplied as parameters to the function.

14 Xiaojuan Cai Computational Thinking 14 drawBar function def drawBar(window, year, height): bar = Rectangle(Point(year, 0), Point(year+1, height)) bar.setFill("green") bar.setWidth(2) bar.draw(window) Why send the window variable as param? Since the GraphWin in the variable win is created inside of main, it is not directly accessible in drawBar.

15 Xiaojuan Cai Computational Thinking 15 Scoping rule The scope of a variable refers to the places in a program a given variable can be referenced. The variables used inside of one function are local to that function. The only way to see a variable from another function is passing it as a parameter. x,y = 0,0 def f(x): y = 1 print x,y f(10) print x,y

16 Xiaojuan Cai Computational Thinking 16 Global variables >>> x = 1 >>> def f(): print x x = 2 >>> f() >>> def h(): global x print x x = 2 print x >>> h() f() x h() x x

17 Xiaojuan Cai Computational Thinking 17 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

18 Xiaojuan Cai Computational Thinking 18 Return values Params are “inputs” of a function Return values are “outputs” of a function discRt = math.sqrt(b*b – 4*a*c) def square(x): return x*x Functions without a return hand back a special object, denoted None. Triangle2.py

19 Xiaojuan Cai Computational Thinking 19 Modifying params Sometimes, we can communicate back to the caller by making changes to the function parameters. def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount

20 Xiaojuan Cai Computational Thinking 20 What went wrong?

21 Xiaojuan Cai Computational Thinking 21 What went wrong?

22 Xiaojuan Cai Computational Thinking 22 The same problem as alias The formal parameters of a function only receive the values of the actual parameters.

23 Xiaojuan Cai Computational Thinking 23 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

24 Xiaojuan Cai Computational Thinking 24 Program structure So far, functions have been used as a mechanism for reducing code duplication. Another reason to use functions is to make your programs more modular. (Even though the amount of code increases) Example: 99-bottle-of-wine

25 Xiaojuan Cai Computational Thinking 25 Conclusion A function is a kind of subprogram. A call to a function initiates a four-step process. 1. caller suspend, 2. assign the formal params, 3. execute the callee, 4. return to the caller The scope of a variable is where it can be referenced. Functions can return values. Python passes parameters by value.


Download ppt "Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015."

Similar presentations


Ads by Google