In-class notes for 02/19/2014
CS 121B (CS1), Spring 2014
Quiz...
Submitted questions on assignments and technology
Upcoming
Homework 5 (to appear)
Submitted questions on readings
Functions, continued
Recall terms:
A function is a procedure of computer actions, which may have 0 or more arguments and 0 or 1 return value
A function call is a language expression for causing the CPU to carry out a function's instructions.
An operator is a function with specialized syntax for its calls. For example, for the addition operator
+
, we write3+4
instead of+(3,4)
Example function definitions in Python3:
def mean(num1, num2): return (num1 + num2)/2 def invite(namelist): for name in namelist: print("Hi,", name, "-- please come to my party!")
Here are sample calls for these functionsmean(5, 8) --> 6.5 invite(['Bob', 'Carol', 'Ted', 'Alice']) Hi, Bob -- please come to my party! Hi, Carol -- please come to my party! Hi, Ted -- please come to my party! Hi, Alice -- please come to my party!
Here, the notation-->
indicates the return value from the callmean(5, 8)
. Having a return value means that the Python3 expressionmean(5, 8)
will be treated as the value 6.5 when the code is executed.Note that a call to
invite()
has no return value, but prints the lines indicated.A
return
statement is used to return a value from a function. If there is no return value, or if no value is supplied for areturn
statement, then there will be no return value from a call of that function.Here is an alternate definition of
invite()
that illustrates how to usereturn
without a value.def invite(namelist): for name in namelist: print("Hi,", name, "-- please come to my party!") return
This definition ofinvite()
performs the same computation as the original definition, but this second one is preferable because it explicitly shows that there is no return value. This benefits a human reader.Specs for functions. A specification or "spec" for a function describes the arguments, return value (if any), and other effects of a function.
Spec for
mean()
:mean
- 2 arguments: Numbers
- Return: A number, the mean (average) of arg1 and arg2
Observe that the return-value description uses arg1 and arg2 to refer to the argument values, in order.
The function
invite()
prints output, but doesn't have a return value. Here is a spec forinvite()
:invite
- One argument: A list of strings
- State change:
- An invitation is printed for each name in arg1.
- Return: None
invite()
) are examples of state changes.Turtle graphics functions (typically no return values needed)
How functions work: binding and evaluation
An assignment such as
x = 7
involves a variable (named location in main memory) and a Python3 value. The effect of this particular assignment is to store the value 7 in the variable location namedx
.Assignment is also called binding -- associating a name with a value
A function definition does not cause cause that function's actions to take place. For example, the
def
statementdef mean(num1, num2): return (num1 + num2)/2
does not itself calculate an average. Instead, that computation is performed later when that function is called, e.g.,mean(5, 8)
When the CPU processes a function call,the values 5 and 8 are assigned to the names
num1
andnum2
, respectively (binding)then, the computation
(num1 + num2)/2
is performedFinally, the resulting value (6.5) is returned. This means that the expression
mean(5, 8)
is replaced by the return value6.5
Random numbers
The functions
random()
andrandrange()
, which are defined in the standard module namedrandom
, generate random floats (between 0 and 1) and random integers (in a specified range)random.random
- No arguments:
-
Return: A random
float
between 0 and 1.
random.randrange
- 2 arguments: Integers
- Return: A random integer x between arg1 and arg2, potentially
import random
in order to access these functionsrandom.random()
andrandom.randrange()
.
Accumulators; memory diagrams
______Local vs. global variables
______if
and guards
______
< >