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

Post on 21-Jan-2016

225 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

Xiaojuan Cai Computational Thinking 3

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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

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

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

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.

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.

Xiaojuan Cai Computational Thinking 9

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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)

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.

Xiaojuan Cai Computational Thinking 12

Function calls illustration

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

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

Xiaojuan Cai Computational Thinking 17

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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

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

Xiaojuan Cai Computational Thinking 20

What went wrong?

Xiaojuan Cai Computational Thinking 21

What went wrong?

Xiaojuan Cai Computational Thinking 22

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

only receive the values of the actual

parameters.

Xiaojuan Cai Computational Thinking 23

Roadmap

• Functions, informally

• Function definition and call

• Getting results from functions

• Return values

• Modify params

• Functions and program structure

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

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.

top related