A function is a worker-bee that
1) takes in some data, called parameters or arguments 2) performs some computations 3) returns a result (optional)
A function definition looks something like:
def func(param1,param2): # computations return result
Once a function is defined, it can be called from 1) your main program code, or 2) another function. For the sample above, a call would look like:
res = func(p1,p2)
What effect can a function have on the world? It can:
- Produce a return value.
- Modify parameters if they are objects (e.g., a list). It cannot modify scalar params.
- Modify a file/database.
- Print (output) information.
In general, most functions should be purely computational (1-3).
That way callers can use the function to compute things, and display/use results how they see fit.
Definitionsscalar – an atomic value. Integers, floating point numbers, strings, and booleans are types of scalars. object-- Multi-item data, such as a Python list. We'll also learn object-oriented programming and how to define objects with named sub-items. local variable -- a variable whose scope is restricted to a particular function. Temporary, scratch space. Private to a function. parameter -- Data sent to one function from another. formal Parameter -- The parameter defined as part of the function definition, e.g., x in: def cube(x): return x*x*x
actual Parameter -- The actual data sent to a function. It's found in the function call, e.g., 7 in result = cube(7) or y in: y=5 res = cube(y) The actual parameters are connected to the formal parameters by order, not by name.
Pass-by-Copy parameter passing-- the value of the actual parameter is copied to the formal parameter. In Python, scalar values are sent by-value. Lists and other objects are sent by reference. How would the following be traced?
def processNumber(x): x=72 return x+3 # main y=54 res = processNumber(y)
Pass-by-Reference parameter passing-- a reference to the actual parameter is sent to the function. When we trace a program, and a list is sent, we don't copy the list to the actual parameter box, we draw an arrow from formal to actual. How would the following then be traced?
def processList(list): list[1]=99 # main aList = [5,2,9] processList(aList)
1. Consider the programs below. Trace them with pencil and paper by drawing boxes for each variable and 'executing' each statement of the program. Also, show what is printed out for the program a . def increment(x): x=x+1 # main program x = 3 print x increment(x) print x b. def increment(x): z = 45 x = x+1 return x # main y = 3 print y y = increment(y) print y q=77 print q increment(q) print q print x print z c. def incrementList(list): i=0 while i<len(list): list[i]=list[i]+1 i=i+1 # main list = [1,2,3] print list incrementList(list) print list 2. Suppose you were asked to write a function 'switch' which switched the value of two variables, e.g., given: x= 5 y= 7 switch(x,y) The goal is that switch would make y=5 and x=7. Can you write such a function? 3. Write a function, with pencil and paper, that doubles the value of each element of a list. Write the function twice, once with the list as a parameter, and once with it as a return value.
4. Suppose you were asked to write a function that doubles the value of an integer. Could you do it with the integer as a parameter or as a return value? 5. Trace the following program using boxes and arrows. Trace it directly to the right of the code. def reverse (list, num): size = len(list) i=0 while i<num: temp = list[size-1-i] list[size-1-i]=list[i] list[i]=temp i=i+1 num=num+1 mylist=[1,2,3,4,5,6] num=3 reverse(mylist,num) print mylist print num 6. Circle and label the following in the code above:
formal parameters actual parameters scalar variables pass-by-value parameters pass-by-reference parameters local variables |
Attachments (1)
-
increment.py - on Oct 5, 2009 12:44 PM by David Wolber (version 1)
1k
Download
|