xiaojuan cai computational thinking 1 lecture 6 defining functions xiaojuan cai (蔡小娟)...

23
Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai 蔡蔡蔡蔡 () [email protected] Fall, 2015

Upload: hilary-lewis

Post on 21-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

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

Xiaojuan Cai Computational Thinking 1

Lecture 6

Defining Functions

Xiaojuan Cai(蔡小娟)

[email protected]

Fall, 2015

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

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.

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

Xiaojuan Cai Computational Thinking 3

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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

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).

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

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())

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

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!“

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

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.

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

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.

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

Xiaojuan Cai Computational Thinking 9

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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

Xiaojuan Cai Computational Thinking 10

Functions, formally• A function definition looks like this:

def <name>(<formal-parameters>):

<body>

• 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)

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

Xiaojuan Cai Computational Thinking 11

Function calls• <name>(<actual-parameters>)

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

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

Xiaojuan Cai Computational Thinking 12

Function calls illustration

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

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

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

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

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

Xiaojuan Cai Computational Thinking 17

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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

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

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

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

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

Xiaojuan Cai Computational Thinking 20

What went wrong?

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

Xiaojuan Cai Computational Thinking 21

What went wrong?

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

Xiaojuan Cai Computational Thinking 22

The same problem as alias• The formal parameters of a function

only receive the values of the actual

parameters.

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

Xiaojuan Cai Computational Thinking 23

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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

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

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

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.