In-class notes for 02/19/2014 (CS 121B (CS1), Spring 2014)
>>     < >




In-class notes for 02/19/2014

CS 121B (CS1), Spring 2014

Submitted questions on assignments and technology

Upcoming

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 write 3+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 functions
        mean(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 call mean(5, 8). Having a return value means that the Python3 expression mean(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 a return 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 use return without a value.

        def invite(namelist):
            for name in namelist:
                print("Hi,", name, "-- please come to my party!")
            return    
    
    This definition of invite() 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 for invite():

    invite

    One argument: A list of strings
    State change:
    An invitation is printed for each name in arg1.
    Return: None
    A state change is an effect of a function other than a return value. Input and output (as in 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 named x.

  • 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 statement

        def 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 and num2, respectively (binding)

    • then, the computation (num1 + num2)/2 is performed

    • Finally, the resulting value (6.5) is returned. This means that the expression

          mean(5, 8)
      
      is replaced by the return value
          6.5
      

Random numbers

  • The functions random() and randrange(), which are defined in the standard module named random, 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
    Note: In these specs, the module name is included before the function name. Be sure to enter import random in order to access these functions random.random() and random.randrange().

Accumulators; memory diagrams

______

Local vs. global variables

______

if and guards

______


< >