1 functions samuel marateck ©2010. 2 a function is a set of statements that can be referred by the...

37
1 Functions Samuel Marateck ©2010

Post on 20-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

1

Functions

Samuel Marateck ©2010

Page 2: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

2

A function is a set of statements that can be

referred by the function name. To start

writing a function.

1. In the File menu in the shell choose New Window.

2. In the new window start by writing the header

def name():

where name is the name you choose for your function

Page 3: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

3

The following is a function that will print NYU 10 times.def tenTimes(): for j in range(10): print(‘NYU’)Note that the header starts with def and ends with a colon. The function name must be followed by (). We will later see what the() are used for.

Page 4: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

4

The colon here serves the same function as

it does for the if statement. It automatically

indents the following statements.

def tenTimes():

for j in range(10):

print(‘NYU’)

Page 5: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

5

Before you execute the function, you must

save it. Do this by choosing Save on the File

menu and then Run Menu on the Run menu.

When you do this, you will be taken to the

shell where you will see >>>

Page 6: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

6

Type the function name and ().

>>>tenTimes().

This will be followed by the output.

Page 7: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

7

In order to run the function from the new

window do the following:

def tenTimes():

for j in range(10):

print(‘NYU’)

tenTimes()

Page 8: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

8

Now when you hit Run Module, the output

will appear in the shell.

Although this is a convenient way of running

the function, it will not work if you want to

execute many functions.

Page 9: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

9

The best way of running the program is to write a main() function as shown here.def tenTimes(): for j in range(10): print(‘NYU’)Def main(): tenTimes()main()

Page 10: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

10

Again when you hit Run Module, the output will be displayed in the shell.You don’t have to call the method that calls the function, main() as we see here. We call it duh().def tenTimes(): for j in range(10): print(‘NYU’)Def duh(): tenTimes()duh()

Page 11: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

11

If you wanted the upper limit of the for loop

to be fed to the function, you could do it by

using a parameter, the entity appearing

between the (). Thus you would write,

Page 12: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

12

def tenTimes(n):

for j in range(n):

print(‘NYU’)

def main():

m = 10

tenTimes(m)

main()

Page 13: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

13

Here m is the actual parameter and n is the

dummy or formal parameter. The value of m

is copied into n. This is a one-way process

as we will soon explain.

The actual and dummy parameters do not

have to be different variables.

Page 14: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

14

Here both the actual and dummy parameters aren.def tenTimes(n): for j in range(n): print(‘NYU’)Def main(): n = 10 tenTimes(n)main()

Page 15: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

15

Just as before there are two locations. The n in main() labels a different location than n in tenTimes does.

Now let’s see what happens if we change the value of the dummy parameter in a function if it changes the value of the actual parameter

Page 16: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

16

Now let’s see what happens if we change the value of the dummy parameter in a function if it changes the value of the actual parameter.def change(num): num = 4 def main(): n = 8 change(n) print(n) main()What value of n is printed in main()?

Page 17: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

17

What value of n is printed in main()?def change(num): num = 4 def main(): n = 8 change(n) print(n) main() The function main() prints 8

Page 18: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

18

Now we see a function with two parameters:

def f(x, y):

print(x + y**2)

def main():

f(1, 3)

main()

Page 19: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

19

def f(x, y):

print(x + y**2)

def main():

f(1, 3)

main()

The order of the 1 and 3 determined what is

printed f(3,1) would give a different answer.

Page 20: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

20

def f(x, y):

print(x + y**2)

def main():

f(3, 1)

main()

Page 21: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

21

What happens if we want a function to return

a value. Let’s write a function that returns

the sum of integers to n:

Page 22: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

22

What happens if we want a function to return a value. Let’s write a function that returns the sum of integers to n:def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sumThe return statement returns execution to the main() function.

Page 23: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

23

How do we write main() so that we can use

add() in main()?

Page 24: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

24

How do we write main() so that we can use add() in main()?def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sumdef main(): total = add(5)

print(total)main()

We use a return statement at the end of the function add. This returnsthe execution to add(5) and the result is assigned to total.

Page 25: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

25

Can we write main() in another way so that

we can call add(n)?

def add(n):

sum = 0

for j in range(1,n + 1):

sum = sum + j

return sum

Page 26: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

26

Can we write main() in another way?def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sumdef main():

print(add(5))main()

Page 27: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

27

Now we call add() in the print().def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sumdef main():

print(add(5))main()The value returned to add(5) is printed in main().

Page 28: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

28

Whenever the results of a function are

assigned to a variable in the calling function,

here main(), or used in a print(), the function

must have a return statement. Let’s see

what happens if we forget to use a return

statement

Page 29: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

29

def increment(x):

y = x + 1

def main():

z = increment(x)

print(z)

main()

Page 30: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

30

def increment(x): y = x + 1def main(): z = increment(x)

print(z)main()Since increment doesn’t return any value, nothing can be assigned to z. What is printed?

Page 31: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

31

def increment(x): y = x + 1def main(): z = increment(x)

print(z)main()Since increment doesn’t return any value, nothing can be assigned to z. What is printed?

The computer prints None.

Page 32: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

32

We can use more than one return in a function:def greater(a, b): if a > b: return a

else: return b def main(): print(greater(3, 7)) main()

Page 33: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

33

Using a default parameter:def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3) print(z)main()

Page 34: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

34

Using a default parameter:def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3) print(z)main() The actual parameters are now 3, and by default, 6

Page 35: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

35

Using a default parameter:def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3) print(z)main() The default parameters must follow the regularparameter, so def greater( a = 6, b): causes an error.

Page 36: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

36

Using a default parameter. If we use two actual parameters, they override the default parameter.def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3,8) print(z)main() The value 8 is printed.

Page 37: 1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the

37

The main() function can call more than one function:def greater(a, b): if a > b: return a else: return b def f(x,y): z = x + y**2 return z def main(): u = greater(3, 7) print(u) s = f(3,4) print(s)main()