topics: function intitution algorithm example function definition (and indentation) function call...

38
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments Return

Upload: felicity-parker

Post on 04-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Topics: Function Intitution

Algorithm Example

Function Definition (and Indentation)

Function Call

Naming and Binding

Local Variables

Function Arguments

Return

Page 2: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Functions

input

function

output

Arguments

Return

Page 3: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Control FlowThe term control flow is typically used to

describe the order of execution in code

This can be represented as a flow chartThe End you will see in the flowcharts is not an

actual programming construct. It merely represents where the chart ends.

In simple programs code flows from the first line to the last line, one line at a time

Page 4: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

a = 15

b = -7

print(a)

print(b)

print(a + b)

End

Page 5: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Intuition behind functionsWe want a mechanisms to allow for code reuse

Consider if we have a program A and we want to write a new program that does what A does only twice

a = 15

b = -7

print(a+b)

Page 6: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Program A’

a = 15

b = -7

print(a+b)

a = 15

b = -7

print(a+b)

Page 7: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

We can use a function!def A():

a = 15

b = -7

print(a+b)

A()

A()

Abstractly a function provides a name to a piece of code

Page 8: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Algorithm Example: Read a book

1. Remove book from bag

2. Place book on desk

3. Open book to first page

4. Until end of book, Read()

Let us now look at step 4 as a function call

Page 9: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

FunctionsRead():

1. Place eyes on top left corner of page

2. For each word on page3. ReadWord()

4. If end of page is reached, change current page to next page

Page 10: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Defining A FunctionFunction definitions start with def

This is followed by the function name, and any arguments the function should have

The : identifies the start of a block of code

The body of the function is anything indented beneath this line

def myFunction(a,b):print(a)print(b)

Page 11: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

ArgumentsFunctions have an important feature

They can receive and return valuesValues are ‘passed to’ a function inside of ( ) when

called and returned using a special instruction

def myFunction( input_values ):return output_value

Page 12: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

FunctionsFunctions come up in two ways in programs

Function DefinitionWhere the function is createdStarts with def in python

Function CallWhere the function is usedresult = myFunction(argument1,argument2)

Page 13: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Function ReturnIf a function needs to return a value back to the

place in the code where it is called it uses a return statement

def myFunction(a,b):print(a)print(b)return a+b

Page 14: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Clicker Question:Are these programs equivalent?

A: Yes

B: No

def myFun(a): print(a) return aprint(myFun(4))

def myFun(a): print(a)print (myFun(4))

Page 15: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Function CallOnce the function is defined it can be called as

many times as one likes

If the function has a return it can be stored into a variable at the call

myFunction(6,7)myFunction(4,10)

a = myFunction(6,7)

Page 16: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Example Revisited(but slightly modified)

def myFun(a): print(a) return a+1myFun(myFun(4))

Page 17: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Example Revisited(but slightly modified…

again)

def myFun(a): print(a) return a+1b =myFun(4)myFun(b)

Page 18: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Question:Are these programs

equivalent?

a = 3def myFun(a): print(a)print(a)

a = 3def myFun(a): print(a) print(a)

21

A: yes

B: no

Page 19: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Clicker Question:Are these programs equivalent?

A: Yes

B: No

a = 3def myFun(b): print(b)print(a)myFun(3)

a = 3def myFun(b): print(b) print(b)myFun(3)

Page 20: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

IndentationIndentation is very important in python

In functions it defines where the function ends

Python will give errors if the indentation is incorrect

Page 21: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Are these programs equivalent?

A: Yes

B: No

a = 3def myFun(a): print (a)myFun(4)

a = 3print (a)

Page 22: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Naming and BindingArguments 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

Page 23: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Naming and Binding

a = 3def myFun(a): print (a)myFun(4)print(a)

Page 24: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Local VariablesWe saw that arguments can be rebound for the

duration of the callWhat about variables that we define in the

function body?

Page 25: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Local VariablesAbstractly there are two types of local variables

1) those which introduce a new variable2) 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

Page 26: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Local Variables

a = 3y = 10def myFun(a): print (a) y = 1myFun(4)print(a)print(y)

Page 27: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Clicker Question:Does this program print 3 or 4?

A: 3

B: 4

x = 3def myFun(): print (x)x = 4myFun()

Page 28: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Question: does this program print 3 or 4?

x = 3def myFun(): print (x)x = 4myFun()

A: 3

B: 4

Page 29: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Variables and FunctionsVariables 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

Page 30: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Variables and Functions

x = 3def myFun(): print (x)x = 4myFun()x =5myFun()

Page 31: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Lets ReviewFunctions take input and produce output

Output is provided by the “return” statementOtherwise the function does not provide output

At the call site of the function the arguments get boundThe 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!

Page 32: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Function ArgumentsFunctions typically assume something important

about the arguments

Will this work no matter what we provide as arguments?

def sumOfTwo(a,b):return a+b

Page 33: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Function ArgumentsConsider 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)

Page 34: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Function ArgumentsThere 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

Page 35: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Function DocumentationThis solution uses comments and if-statements.

We will revisit this in later slides

# This function expects two integers # and returns -1 otherwisedef sumOfTwo(a,b):

if type(a) == int and type(b) == int : return a+b

return -1

Page 36: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

What all can I do within the body of a function?

Define local variables

Call other functions

Define new functions

Page 37: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

Function calls as conditions?

We have seen a lot of conditionals like: if x == 3: or if y: or if (x<3) and (y%3 ==0):

But what about something like this?

if myFun(x):

This is equivalent to writing:

z = myFun(x):

if z:

So it is just fine

Page 38: Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments

ReturnsIf a function does not specify a value to return it

returns a special python value: “None”

Just because a function has a return statement in it, does NOT mean it will return a value in every case

>>> def example(x): if x < 10:

print(x) else:

return(30)>>> z = example(10)>>> print(z)30>>> z = example(9)9>>> print(z)None