46
Q and A for Section 5.2 CS 106 Victor Norman

Q and A for Section 5.2

  • Upload
    theo

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

Q and A for Section 5.2. CS 106. Function definition syntax. Q: The syntax for the definition of a function is: def ______________( _______________ ): _____________ A: def < functionname > ( < param list> ): - PowerPoint PPT Presentation

Citation preview

Page 1: Q and A for Section 5.2

Q and A for Section 5.2

CS 106Victor Norman

Page 2: Q and A for Section 5.2

Function definition, call syntax

def <functionname> ( <param list> ): <body>Note: <param list> is a comma-separated list of

identifiers, called parameters, that will refer to the values passed in

Function call:<functionname>(<value>, <value>)result = <functionname>(<value>, <value>, …)<values> are called arguments.

Page 3: Q and A for Section 5.2

Metaphor: Bank Teller

• A bank teller does work for you.• You hand him some information (money or a paycheck or

a request for money).– input parameters

• He does some work for you using your inputs.– code in the function

• He hands back some results to you (money or a receipt or both).– return value or values.

• You don’t really know exactly what he is doing, but that’s ok.

Page 4: Q and A for Section 5.2

Why create functions?

• A function can be called a sub-program.– It takes inputs, does some computation using

those inputs, and produces outputs.• A function can be given a really nice name.

def filterOutNonExcelFiles(fileList): <code>

excelFiles = filterOutNonExcelFiles(allFiles)

– This makes the main code much easier to understand – i.e., more hospitable.

Page 5: Q and A for Section 5.2

Why create functions?

• Thus, a function is an abstraction of code.– we don’t know how math.sin() works, but we can use it

anyway.• A function, once debugged, can be reused many times

without worrying about whether the code is correct or not.– Code reuse.– Should help with debugging large programs.

• A function operates in its own little world.– Variables you create in the function are not accessible from

outside – they have local scope.

Page 6: Q and A for Section 5.2

Function definition vs. call

• You have to define a function before you can use it.

def isOdd(num): “““Return True if num is an odd number, False if it is even.””” if num % 2 == 1: return True return False

• Function call:x = int(raw_input(“Enter a number: “))if isOdd(x): print x, “is an odd number.”

Page 7: Q and A for Section 5.2

Multiple returns in a function defn

Q: Is it possible to use the return statement multiple times in the body of a function?

A: Yes! If there are different places where you have determined what to return, just call return there.

Page 8: Q and A for Section 5.2

Let’s define some functions!incr(x)isPositive(aNum)half(val)abs(y)area(w, h)max(val1, val2)min(val1, val2, val3)

Functions on lists:

incrAll(nums)listAbs(nums)removeNegs(nums)concatAll(listOfStr)

Page 9: Q and A for Section 5.2

Where to define functions?

Q: Where do you define functions? In the same file? In another place?

A: Either in the same file or in another file. You can build up a file with some useful functions that you will use in multiple projects. You would have to import the file to use the functions.Or you can define functions in the same file, for use only in that file.

Page 10: Q and A for Section 5.2

File organization# Comments about what this code does.# Author information, perhaps.

# ------- imports -------import sysfrom math import sin, cos

# ------- CONSTANT definitions -------WORLD_SIZE = 500HOUSE_SIZE = 20

# ------- Function definitions -------def isOdd(x): “””blah””” <code here>

# -------- main code ---------num_houses = int(raw_input(“Enter how many houses to draw: “))… etc …

Page 11: Q and A for Section 5.2

What does this function do?

Q: What does this function do?def unknown(val1, val2): tmp = val1 ** val2

return tmp

A: It is the power function.Note: tmp is a local variable – only defined and

accessible within the function.

Page 12: Q and A for Section 5.2

Function call example

Q: Write code to call unknown() and print the results.

A: print "An unknown result: ", unknown(2.5, 3.5)

What if unknown() had printed a result instead of returning it?...

Page 13: Q and A for Section 5.2

return statement

Q: What does return someVal do?A: It returns the value someVal to the caller, and, ends execution of code in the function.

Q: How is this different than printing a value in a function?

A: Printing sends out characters to the screen, but does not give a value back to the caller.

Page 14: Q and A for Section 5.2

return vs. break

Q: Is return similar to break in that it gets you out of a loop?

A: Yes! Very observant of you. A return is another way to get out of an “infinite loop”, but, of course, it can only be used in a function.

Page 15: Q and A for Section 5.2

Exampledef isPrime(n): “””return True if n is prime, False otherwise””” res = True # assume n is prime # try every division of n up to n / 2. for div in range(n / 2 + 1): if n % div == 0: # We found a divisor that divides n # perfectly. Therefore, we know n is # not prime. So return immediately res = False break # We’ve tried every divisor and not found any # that divides n perfectly. Therefore, n must # be prime. return res

Page 16: Q and A for Section 5.2

Exampledef isPrime(n): “””return True if n is prime, False otherwise””” # try every division of n up to n / 2. for div in range(n / 2 + 1): if n % div == 0: # We found a divisor that divides n # perfectly. Therefore, we know n is # not prime. So return immediately return False # We’ve tried every divisor and not found any # that divides n perfectly. Therefore, n must # be prime. return True

Page 17: Q and A for Section 5.2

What is the output?

Q: What is the output from this code:def f(x): x += 3 print xz = 10f(z)print z

A: Output is 13 on one line, then 10 on the next. Why?

Page 18: Q and A for Section 5.2

What is the result?

Q: What does z hold at the end of this code?:def add3(x): x += 3 print xy = 7z = add3(y)

A: z holds the value None, because the function f() does not return anything. NOTE: printing a value is NOT the same as returning a value!

Page 19: Q and A for Section 5.2

Scope

Q: If you define a variable inside a function, can you access that variable from outside the function? Explain.

A: No, you cannot access it. Once the code stops running the function, the local variables cease to exist.

Page 20: Q and A for Section 5.2

Activation Records

• When a function is called, an activation record for that function is defined and pushed on the “stack”.– The parameters refer to the values passed in as

arguments.– Local variables are created in the activation record.

• When the function returns, the activation record is deleted.

• If a function calls another function, another activation record for that function is added.

Page 21: Q and A for Section 5.2

Local vs. Global Scope

• A local variable defined in a function definition has local scope. – It cannot be accessed from outside.– Same for parameters of the function definition.

• A global variable can be accessed within a function:– If the variable is not found already defined in the

function, then the code looks in the global scope for the definition.

– A global variable cannot be set from within a function.

Page 22: Q and A for Section 5.2

Whiteboard: draw this

def abc(x, y): t = x * 3 + y return tdef dfg(z): res = abc(z, z + 1) return resx = dfg(3)print x

Page 23: Q and A for Section 5.2

Mistakes students make (1)

• (Sung to the tune “Mistakes lovers make”)def area(w, h): w = 10 h = 30 return w * h

• Ignores the input parameters.

Page 24: Q and A for Section 5.2

Mistakes students make (2)

def area(w, h): print w * htheArea = area(10, 100)

• Print instead of return the computed value

Page 25: Q and A for Section 5.2

Mistakes students make (3)

def volume(w, h, 10): return w * h * 10

• Function definition problems – not using identifiers only in the param list.

Page 26: Q and A for Section 5.2

Mistakes students make (4)

def volume(w, h, d): w * h * d

• Forgetting to return a value.

def volume(w, h, d): return w * h * d

volume(30, 77.2, 33)

• Forgetting to save the returned value.

Page 27: Q and A for Section 5.2

Mistakes students make (5)

def searchForString(aList, aStr): <code>

index = searchForString(listOfQuotes, aQuote)

• Thinking that the name of the parameter must match the name of the argument.

Page 28: Q and A for Section 5.2

OK? Or not?

Q: Will this work?def average(x, y): return (x + y) / 2.0 z = 10q = average(z, z)

A: Yes. Why not?!

Page 29: Q and A for Section 5.2

Formal/actual parameters

Q: In this code, identify the formal parameters and the actual parameters.

def average(x, y): return (x + y) / 2.0 z = 10q = average(z, z)

A: x, y are the formal parameters. z and z are the actual parameters.

Page 30: Q and A for Section 5.2

Global/local names

Q: Is this legal?def average(x, y): return (x + y) / 2.0y = 10x = 20print average(y, x)

A: Yes. The value of y is assigned to x in average(). The value in x is assigned to y in average().

Page 31: Q and A for Section 5.2

Creating global variables from inside a function?

Q: Is there any way to set variables withint he body of a function that the uer could use later, or are they all local scope?A: You can do this, but please don’t, if at all possible.You can use the keyword global:def a(x, y): global z z = x + yprint z # errora(3, 4)print z # prints 7

Page 32: Q and A for Section 5.2

Number of parameters

Q: Does a function need to know how many variables it is taking in? So, can you write a function that takes in an undetermined amount of variables?A: (Q should use the term “parameters”, not variables.)A function has to know how many parameters it has coming in. It is basing its output on its input parameters.

Page 33: Q and A for Section 5.2

Undetermined # of params?

Q: Can you write a function that takes in an undetermined number of parameters?A: You can… but you don’t want to… in this class…e.g., it is nice to have a function max() that takes any number of parameters:max(1, 7, 22, -33)max(-33, -1000, math.pi)max(1, 2, 3, 4, 5, 6, 7, 8, 8, 10, 11, 12, 13, 14, 15, 176)

Page 34: Q and A for Section 5.2

Fruitless function?

Q: When might you write and use a function that doesn't return anything?

A: Perhaps the function prints out some stuff.Or this:def add1(x): x.append(1)lst = [ 7 ]add1(lst)# what is lst now?

Page 35: Q and A for Section 5.2

Legal?

Q: Is this legal?def average(x, y): return (x + y + z) / 3.0z = 100000print average(0, 1)

A: It is legal. A function can access a variable in the global scope. This is often not very desirable, however.

Page 36: Q and A for Section 5.2

Functions calling other functions

Q: Can a function call another function? Can a function call itself?

A: Of course! And, yes!A function that calls itself is called a recursive

function. Don’t try this at home…

Page 37: Q and A for Section 5.2

Interface vs. Implementation

Q: Does a person have to know how a function does something in order to use it?

A: No! A function defines what it does, and the caller doesn't care how it does it.

Page 38: Q and A for Section 5.2

Knowing what a function does

Q: How does a person know what a function can do for you?

A: The writer of the function gives the function a nice descriptive name, with good formal parameter names, and a docstring (or comments) to describe what the function does.

Page 39: Q and A for Section 5.2

Flow of Control

• Flow of control is how the thread of execution jumps around the code. I.e., what lines are being executed, when.

• Starts in the main code. • Each time a function is called, execution

“flows” to that function definition. Control flows back to the caller when the function returns.

Page 40: Q and A for Section 5.2

Flow of control example1 def khvr(a):2 a *= 2.53 return a4 def se(x):5 y = khvr(x + 1)6 return y – 17 def rvd(p):8 return se(p ** 2)9 # main code10 print rvd(2)

Control flow1 function defn4 function defn7 function defn10 rvd called8 se called5 khvr called2 khvr body3 return from khvr to se6 return from se to rvd8 return from rvd to main10 print result

Page 41: Q and A for Section 5.2

Optional Parameters

• With “normal” function definitions, each parameter has to be given a value when the function is called.

def aFunc(x, y, z): # 3 parameters <code>aFunc(3, 4, “Hi, Isaac!”) # 3 values

Page 42: Q and A for Section 5.2

Optional Parameters (2)

• With optional parameters, you can specify in the function definition the default value for parameters when the caller provides no value.

def countdown(start = 10): “””print a countdown, starting at the given start value (10 by default).””” for i in range(start, 0, -1): print i

countdown() # no value given, so start is 10countdown(20) # use 20 for start

Page 43: Q and A for Section 5.2

Optional Parameters (3)

• Notice that the code in the function is not “special” – uses start normally.

• Optional parameters (with default values) must be specified after non-optional (“required”) parameters.

Page 44: Q and A for Section 5.2

Using optional params

Q: If you are given this function, show two ways you could call it, with different numbers of arguments:

def plot_points(x_list, y_list, symbol='ro')

A: plot_points(xs, ys)plot_points(xs, ys, 'g-')

Page 45: Q and A for Section 5.2

draw() signature

Q: Write the signature of a function draw() that takes x, y, and radius as 3 required parameters, and takes canvas and width as optional parameters, with the default value for canvas being canv and the default value for width being 2.

A: draw(x, y, radius, canvas=canv, width=2)

Page 46: Q and A for Section 5.2

Checking input parameters

Q: What are the reasons for checking input parameters to functions?A: Sometimes functions cannot operate if sent the wrong kinds of parameters – wrong values or wrong types. Checking input parameters gives better feedback to the programmer than just a SyntaxError or DivideByZero error.