# Intro to Functional Decomposition

• We will do Homework 1 in class as a way to learn about functions.
• Functional decomposition is a technique for developing software in which the problem is divided into more easily handled subprograms, the solutions of which create a solution to the overall problem.
• Ie break the problem into smaller problems.
• This may be recursive if the smaller problems are not small enough.
• This is sometimes called
• structural design
• top down design
• step wise refinement
• modular programming.
• Procedural programming
• This is not functional programming.
• The idea is to break it down until the little parts become easy.
• We could have two results from this process
• A concrete step where the implementation details are fully specified.
• An abstract step where some implementation details remain unspecified.
• Think about the following possible solution to the Caesar Cipher program
• Ask the user if they wish to encrypt a message
While the user wants to
get the shift amount
get the plain text
encode the plain text
print the result
Ask the user if they wish to encrypt a message 
• Is this a reasonable first step in solving this problem?
• Will it work?
• Is it concrete or abstract?
• While there is not a good response
Prompt with "Do you wish to encrypt a message (yes/no)? "
Read in the entire line of the response.
Print a newline after you read in the line.
Convert the input line to all lower case. (Yes, YeS and YES are all ok answers).

• Is this a reasonable first step in solving this problem?
• Will it work?
• Is it concrete or abstract?
• In general, we strive to build cohesive functions
• A function is cohesive if all concrete steps are directed toward solving just one problem and any other significant sub problems are written as abstract steps.
• The function does just one thing.
• Generally is is either abstract or concrete.
• She gives some steps to writing a cohesive function
1. Think about ho you would solve the sub problem by hand
2. Write down the major steps
3. If a step is simple enough that you can implement it directly in C++ it is at the concrete level and needs no further refinement.
4. If you have to think about how to implement a step as a series of smaller steps, or as several c++ statements, it is still at an abstract level.
5. If you are trying to write a series of steps and start to feel overwhelmed by details, you probably are bypassing one or more levels of abstraction. Stand back and look for pieces that you can write as more abstract steps.
• Just a note on abstraction
• We generally mean focusing on details that are important and ignoring others.
• "The essence of abstraction is preserving information that is relevant in a given context, and forgetting information that is irrelevant in that context.", John V. Guttag.
• Note in the first procedure, we don't care about the details for imputing a answer, we just care that we get an answer back. This is an abstract function
• In the second procedure we are very focused on the details of input, but not on how to covert something to upper case.
• Much of programming, and computer science is being able to focus on the right level of abstraction at the right time.
• You need to develop your skills in decomposing problems into smaller problems.
• This IS the way that code is written today.