Functional Decomposition
In this lab, you’ll practice breaking down problems into smaller, more manageable chunks using functions.
Summary (Notes)
Programming Language Syntax
- The syntax of a programming language are the rules by which we form legal programs.
- In Python (and most programming languages), there are two classes of syntactic forms: statements and expressions.
- Statements are commands that produce effects, e.g., printing to the screen.
- Expressions are computations that produce a value.
String Literals
- An example of an expression is a string literal which is text surrounded by single or double quotes, for example:
'Hello World'
"This is a string literal"
'First line\nsecond line\nthird line'
"\\ Backslashes \\ and \" Quotes\""
- A string literal is an expression that evaluates to a string value. That string value is the literal text encased in the quotes (hence the name).
- To print special characters within a string literal, we use escape sequences, a backslash followed by a letter:
\n
: newline\t
: tab\
: backslash\'
: single quote\"
: double quote
The Print Statement
- The print statement takes an expression that produces a string value and prints that string to the console along with a newline character. For now, we can only provide our only expression form, a string literal:
print <expression>
- Here is an example of calling
print
to print outHello World!
:
print ('Hello World!')
Function Declaration Statements
- A function is a named body of code that can be invoked at will.
- A function declaration statement introduces a function. It’s syntax is:
def <function name>():
<statement1>
<statement2>
...
- The body of the function is the set of statements it contains. These statements are executed in-order when the function is called.
- Note that the body of the function must be indented (using a tab or at least four spaces) relative to the function body. Unlike other languages, Python is a whitespace-sensitive language; in some places, whitespace actually matters!. The easiest way to maintain correct indentation is to let your editor (in this case Sublime) handle it. For example, when you are writing a new function the moment you hit the enter key after typing in the : you will notice the next line will be indented appropriately for you.
- Here is an example of a function that prints
Hello World!
:
def print_hello():
print ('Hello World!')
- When naming a function, we choose names that describe what the function does. This usually means the name is a verb, e.g., “print”, “save”, or “inject”.
- When naming functions, we use the underscore style where we use all-lowercase letters, separating words with underscores.
Function Call Statements
- One way to invoke functions is with function call statements. Their syntax is:
<function name>()
- For example, to call our
print_hello
function from above we would write:
print_hello()
- When a function is called, the body of the function is executed, line-by-line. After the function is done, we proceed with the statements after the function call.
- Note that functions can call functions, which can call functions, which can call functions, …!
Python Programs
- A Python program is a collection of statements—we have seen print, function declaration, and function call statements.
- A program runs by executing the set of statements it contains in-order, top-to-bottom.
- To organize our code, it is good style to have a function act as an entry point for our code, typically called
main
. - Thus, our programs typically have the following form:
<function declaration>
<function declaration>
...
def main():
<body of main>
main()
The Song of Love
Even with our extremely limited Python skill set we are still able to exercise some of the fundamentals of computational thinking. One of those fundamentals we’ll explore in this homework is the ability to identify structure within a problem that we’re trying to solve and then eliminate redundancy in the code we make to solve that problem.
Problem
A cumulative song song is song whose verses build upon each other. Classic examples of cumulative songs include “There was an Old Lady that Swallowed a Fly” and “The Twelve Days of Christmas”. For this lab, you will be reproducing the lyrics of a more contemporary cumulative song: the “Song of Love” from the Broadway musical comedy Once Upon a Mattress.
For reference, here’s the Song of Love performed by the Bakersfield Theater of Bakersfield, California:
I like you, Fred, I like you!
You're just saying those words to be kind.
No, I mean it. I like... I mean I love you, Fred!
He is out of his medieval mind!
I'm perfectly sane and sound! I never felt better in my life!
Everybody... everybody, everybody! Come on! And meet my incipient wife!
I'm in love with a girl named Fred.
My reasons must be clear.
When she shows you all how strong she is you'll stand right up and cheer!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred! Yeah!
I'm in love with a girl named Fred.
She drinks just like a lord!
So sing a merry drinking song and let the wine be poured!
Fill the bowl to overflowing. Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred! Yeah!
I'm in love with a girl named Fred.
She sings just like a bird!
You'll be left completely speechless when her gentle voice is heard!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing. Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred! Yeah!
I'm in love with a girl named Fred.
She wrestles like a Greek!
You will clap your hands in wonder at her fabulous technique!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing. Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred! Yeah!
I'm in love with a girl named Fred.
She dances with such grace!
You are bound to sing her praises 'til you're purple in the face!
Bravo! Bravo! Bravissimo bravo! Bravissimo!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing. Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred! Yeah!
I'm in love with a girl named Fred.
She's musical to boot!
She will set your feet a-tapping when she plays upon her lute!
Strum strum, strum strum, strum strum strum strum strum, strum.
Bravo! Bravo! Bravissimo bravo! Bravissimo!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing. Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred! Yeah!
I'm in love with a girl named Fred.
A clever, clownish wit!
When she does her funny pantomime your sides are sure to split!
Ha ha ha ha, ho ho ho ho, ha ha ha ha ho!
Strum strum, strum strum, strum strum strum strum strum, strum.
Bravo! Bravo! Bravissimo bravo! Bravissimo!
Clap clap, clap clap, clap clap clap clap, clap, clap clap!
La la la la, la la la la, la la la la la!
Fill the bowl to overflowing. Raise the goblet high!
With a "F" and a "R" and an "E" and a "D"
And a "F-R-E-D" Fred! Yeah!
I'm in love with a girl.
He's in love with a girl named "F-R-E-D" Fred!
Your goal is to write a program to reproduce exactly the lyrics to the Song of Love as presented above. You should preserve capitalization, punctuation, and spacing. As a rule of thumb, a computer will do exactly what you tell it and no better (or worse), so you will need to be diligent about checking the output of your program to ensure that it matches the format above.
Your program should exist in a Python file called songoflove.py with a main function and appropriate helper functions that main calls.
Style
One easy way to do this program is to copy-paste the above lyrics into your Python file and wrap it in print
calls. However, this defeats the purpose of the assignment. Instead you should use functions to both capture the structure of the song and eliminate redundancy of your code.
Capturing structure
As much as possible, your programs should capture the inherent structure of the problem at hand. In this case, your program should reflect the fact that a song can be decomposed into a series of verses. For example, you should have a function for each verse that prints the contents of that verse to the console.
Eliminating redundancy
If you inspect the lyrics, you’ll quickly notice that several lines are repeated, for example, the line I'm in love with a girl named Fred
appears in every verse beyond the first and last. While we could write one print
for each time we print this line to the console, imagine if we made a typo transcribing the line. We would need to visit each of the print
to ensure that we didn’t make the same mistake at each, a code maintenance disaster even by our simple standards!
However, simply factoring out that line into its own method is not enough. You must identify whole groups of lines that are repeated in the song and use functions to eliminate those redundancies. Keep in mind that functions can call other functions. So you’ll need to take a step back and figure out how reduce redundancy not only in groups of repeated print
statements but also groups of repeated functions calls as well.
Diffing Output
To ensure that your output exactly matches the text above, you should use a diffing tool. A diffing tool compares two bodies of text and reports any differences it discovers between them. For this homework, you can use diffchecker to compare the output of your program with the text above.