Lecture 4: Decomposition, Abstraction, Functions

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/15

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

16 Terms

1
New cards

How do we write code?

so far we… open a file → type some code/sequences of instructions (may contain assignments, loops, conditionals,etc.) to solve a particular problem given.. using only a SINGLE file

2
New cards

Problems of only using a Single file

  • Easy for small-scale problems but messy for larger problems

  • hard to keep track of details (to know if the right info is supplied to the right part of code)

3
New cards

Good Programming

more code not necessarily a good thing, instead it’s measured by the amount of functionality (introduce functions!)

4
New cards

Functions

mechanism to achieve decomposition and abstraction; reusable pieces of chunks of code that need to be written; aren’t run in a program until they are “called” or “invoked” in a program

5
New cards

Decomposition

in programming, divide code into modules (achieved with functions and classes); idea is that different devices/ parts of codes (modules) work together to achieve a common end goal; used to create structure

6
New cards

modules

self-contained; used to break up code; intended to be reusable (can be used with different inputs); keeps code organized and coherent

7
New cards

Abstraction

Used to suppress details; in programming, think of a piece of code as a black box (cannot see unnecessary nor wanted tedious coding details) which can be achieved with function specifications or docstrings; idea is to not need to know how program works to use it (just need to know inputs and outputs)

8
New cards

Functions characteristics

  • has a name

  • has parameters (0 or more)

  • a docstring (optional but recommended)

  • a body

  • returns something

9
New cards

Sample function

def (name) (parameters):

(indent) “““specification/docstring(multiline comment)”””

(indent) body(run commands and include return statement with expression to evaluate)

10
New cards

Scope

mapping of names to objects

11
New cards

If NO return statement..

Python returns the value None (not Boolean, NoneType) which represents the absence of a value

12
New cards

return vs print

knowt flashcard image
13
New cards
<p>Define a function with a variable that’s also defined outside of function →</p>

Define a function with a variable that’s also defined outside of function →

no interference

<p>no interference </p>
14
New cards
<p>function called with a variable that’s only defined outside of function →</p>

function called with a variable that’s only defined outside of function →

use outside definition

<p>use outside definition </p>
15
New cards
<p>function tries to increment/modify a variable that’s only defined outside →</p>

function tries to increment/modify a variable that’s only defined outside →

not allowed in Python but could try global variables

<p>not allowed in Python but could try global variables</p>
16
New cards

global variables

not recommended to use in Python but they allow for the modification of variables (that are only defined outside of a function) inside a function (defeats purpose of functions)