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 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
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.
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’)
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 >>>
6 Type the function name and (). >>>tenTimes(). This will be followed by the output.
7 In order to run the function from the new window do the following: def tenTimes(): for j in range(10): print(‘NYU’) tenTimes()
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.
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()
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()
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,
12 def tenTimes(n): for j in range(n): print(‘NYU’) def main(): m = 10 tenTimes(m) main()
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.
14 Here both the actual and dummy parameters are n. def tenTimes(n): for j in range(n): print(‘NYU’) Def main(): n = 10 tenTimes(n) main()
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
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()?
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
18 Now we see a function with two parameters: def f(x, y): print(x + y**2) def main(): f(1, 3) main()
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.
20 def f(x, y): print(x + y**2) def main(): f(3, 1) main()
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:
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 sum The return statement returns execution to the main() function.
23 How do we write main() so that we can use add() in main()?
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 sum def main(): total = add(5) print(total) main() We use a return statement at the end of the function add. This returns the execution to add(5) and the result is assigned to total.
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
26 Can we write main() in another way? def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sum def main(): print(add(5)) main()
27 Now we call add() in the print(). def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sum def main(): print(add(5)) main() The value returned to add(5) is printed in main().
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
29 def increment(x): y = x + 1 def main(): z = increment(x) print(z) main()
30 def increment(x): y = x + 1 def main(): z = increment(x) print(z) main() Since increment doesn’t return any value, nothing can be assigned to z. What is printed?
31 def increment(x): y = x + 1 def 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.
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()
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()
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
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 regular parameter, so def greater( a = 6, b): causes an error.
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.
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()