Chapter 6 Flashcards

1
Q

Functions as abstraction mechanisms

A

-Abstraction- hides detail, allowing person to view many things as one thing
-Example of everyday tasks: doing my laundry

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Functions eliminate__

A

redundancy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Functions hide__

A

complexity
- a function call expresses the idea of a process to the programmer w/o forcing him/her to wade through the complex code that realizes that idea

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Functions Support ___ with Systematic Variations

A
  • General Method: a method that solves a class of problems, not just one individual problem
  • problem instances: individual problems that make up a class of problems
  • Algorithms should be general enough to provide a solution to many problem instances
  • A function should provide a general method with systematic variations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Functions support the ___

A
  • Division of labor
  • Each function should perform a single coherent task
  • Example: Computing a summation
  • Each of the tasks required by a system can be assigned to a function
  • Including the tasks of managing or coordinating the use of other functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Top-down design

A
  • Starts with a global view of the entire problem and breaks the
    problem into smaller, more manageable subproblems (problem decomposition)
  • As each subproblem is isolated, its solution is assigned to a function
  • As functions are developed to solve subproblems, solution to overall problem is gradually filled out (stepwise refinement)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Structure chart

A
  • a diagram that shows the relationship among a program’s
    functions and the passage of data between them
  • Each box in the structure is labeled with a function name
    *The main function at the top is where the design begins
    *Lines connecting the boxes are labeled with data type names
    *Arrows indicate the flow of data between them
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

recursive design

A

the process of decomposing a problem into subproblems of exactly the same form that can be solved by the same algorithm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

recursive function

A

a function that calls itself
-To prevent function from repeating itself indefinitely, it must contain at least one selection statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

base case

A

the condition in a recursive algorithm that is tested to halt the recursive process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

selection statement

A

examines base case to determine whether to stop or to continue with another recursive step.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

recursive definition

A

-a set of statements in which at least one statement is defined in terms of itself.
-consists of equations that state what a value is for one or more
base cases and one or more recursive cases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

infinite recursion

A

-in a running program, the state that occurs when a recursive method cannot reach a stopping state
-arises when programmer fails to specify base case or to
reduce size of problem in a way that terminates the recursive process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

PVM reserves an area of memory for the ____

A

call stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

For each call of a function, the PVM must allocate on the call stack a _____

A

stack frame

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A stack frame contains____

A
  • Values of the arguments
  • Return address for the particular function call
  • Space for the function call’s return value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When a call returns, ________ is used to locate the next instruction, and stack frame is deallocated

A

return address

18
Q

T or F: Amount of memory needed for a loop grows with the size of the problem’s data set

A

F: Amount of memory needed for a loop does not grow with the size of the problem’s data set

19
Q

namespace

A

the set of all of a program’s variables and their values
- can be controlled with good design principles

20
Q

____ and _____ receive their values as soon as they are introduced

A
  • Module variables
  • temporary variables
21
Q

____ behave like a variable and are introduced in a function or method header

A
  • Parameters
  • Do not receive a value until the function is called
22
Q

method names

A

a method reference always uses an object followed by a dot and the method name
ie:
sentence.split()

23
Q

scope

A

the area of a program text in which the value of a variable is visible

24
Q

Scope of temp variables

A
  • Area of code in the body of the function just below where each variable is introduced
  • restricted to the body of the functions in which they are introduced and invisible elsewhere in a module
25
Q

Scope of parameter

A
  • the entire body of the function
  • invisible outside function definition
26
Q

Scope of module variables

A
  • includes entire module below point where they are introduced
  • also includes the nested bodies of other function def that occur earlier
  • A function can reference a module variable, but can’t under normal circumstances assign a new value to it
27
Q

What happens when a Python function assigns a new value to a module variable?

A
  • When such an attempt is made, the PVM creates a new, temporary variable of the same name within the function
  • variable does not change, still remains the value it was assigned outside of function
28
Q

Variable’s lifetime

A

Period of time when variable has memory storage
associated with it
- When a variable comes into existence, storage is allocated for it; when it goes out of existence, storage is reclaimed by the PVM

29
Q

T or F: Module variables come into existence when introduced and generally exist for lifetime of program that introduces or imports them

A

True

30
Q

__ and __ come into existence when bound to values
during call, but go out of existence when call terminates

A

Parameters and temporary variables

31
Q

A functions arguments

A
  • provide the function’s caller with the means of transmitting
    information to the function
32
Q

optional arguments

A
  • can specify optional args with default values in any function definition
  • syntax:
    def <function>(<required>,
    <key‐1> = <val−1>, ... <key−n> = <val−n>)</required></function>
  • Following the required args are one or more default or keyword args
  • When function is called with these args, default values are overridden by caller’s values
33
Q

Default args can be supplied in 2 ways which are?

A
  1. By position
  2. By keyword
34
Q

higher‐order function

A
  • expects a function and a set of data values as arguments and/or returns functions as values
  • Argument function is applied to each data value and a set of results or a single data value is returned
  • separates task of transforming each data value from logic of accumulating the results
35
Q

T/F: Functions can be assigned to variables, passed as arguments, returned as the values of other functions, and stored in data structures

A

True

36
Q

T/F: Passing a function as an argument is different from passing any other datum

A

False: Passing a function as an argument is no different from passing any other datum

37
Q

Mapping

A
  • applies a function to each value in a sequence and returns a new sequence of the results
38
Q

Filtering

A

a function called a predicate is applied to each value in a list
- If predicate returns True, value is added to a new list; otherwise, value is dropped from consideration

39
Q

reducing

A

we take a list of values and repeatedly apply a function to accumulate a single data value

40
Q

Lambda

A
  • an anonymous function
  • When the lambda is applied to its arguments, its expression is evaluated and its value is returned
  • syntax of lambda is very tight and restrictive:
    lambda <argname−1, …, argname−n>: <expression></expression>
  • All of the code must appear on one line and it cannot
    include a selection statement, because selection statements are not expressions
41
Q

jump table

A

is a dictionary of functions keyed by command names