What is a design technique that programmers use to break down an algorithm into functions?
Chapter 3 Review QuestionsMultiple Choice1. A group of statements that exist within a program for the purpose of performing a specifictask is a(n) __________.a. blockb. parameterc.functiond. expression Show
2. A design technique that helps to reduce the duplication of code within a program andis a benefit of using functions is __________. Get answer to your question and much more 3. The first line of a function definition is known as the __________. Get answer to your question and much more 4. You __________ the function to execute it. Get answer to your question and much more 5. A design technique that programmers use to break down an algorithm into functionsis known as __________.a. top-down designb. code simplificationc. code refactoringd. hierarchical subtasking 6. A __________ is a diagram that gives a visual representation of the relationshipsbetween functions in a program. Get answer to your question and much more Functions "Encapsulate" a task (they combine many instructions into a single line of code). Most programming languages provide many built in functions that would otherwise require many steps to accomplish, for example computing the square root of a number. In general, we don't care how a function does what it does, only that it "does it"! When a function is "called" the program "leaves" the current section of code and begins to execute the first line inside the function. Thus the function "flow of control" is:
Why do we Write Functions?
Steps to Writing a Function
Parts of a "black box" (i.e., a function)Functions can be called "black boxes" because we don't need to know how they work. Just what is supposed to go into them, and what is supposed to come out of them. When defining a program as a black box, we must describe the following attributes of the function. Note: most documentation systems are just this, the attributes of a function with no code associated with it.
Function WorkspaceEvery function has its own Workspace. This means that every variable inside the function is only usable during the execution of the function (and then the variables go away). Having a separate "workspace" for each function is critical to proper software engineering. If every function shared every variable in an entire program, it would be easy to inadvertently change the values of variables that you shouldn't. Further, it would be hard to remember what "names" have been used elsewhere, and coming up with new names to represent similar ideas would be challenging. A side-effect of function variables not existing after the end of the function is that the only way to get information "out" of a function is by "returning" that information via the output of the function. Additionally, the function can only "see" the information that is "passed" to it via parameters. Thus the only way information can get "in" to the function is by using parameters. Note: In certain object oriented languages (e.g., C++, Java, ActionScript), a function can also see all of the variables associated with its containing object. Formal vs. Actual ParametersWhen we create a function, it should represent a "generic" action that can be applied in many circumstances. For example, if we want to find the average grade, it doesn't matter if it is on a test, or on a quiz, or an assignment, or a midterm, etc... given any list of grades we can compute an average! ...but if it can be any list of grades, how do we know what the list of grades will be called? The answer: we don't care. You, the programmer of the function, provide your own name for the data. This is much the same as when a sales person calls you and reads a script trying to sell something to you, they say: Dear _insert customer name here_, let me sell you our wonderful product. When writing a function, the programmer must provide a blank to plug in what ever data is of current interest; the blank should have a good symbolic name saying what it will represent. Here is a pseudocode function example: Inside the average_grade function, the name list_of_grades will be used in place of whatever variable some other user has stored his or her grades in. Thus to call the function, I might write: In "My" code, the grades are stored in the variable, "midterm_grades". Inside the function, the grades are stored in the variable "list_of_grades". Thus, during the execution of the program, both names will refer to the same thing but at different times. The parameter "list_of_grades" is called a Formal paramater; again, this just means a place holder name for any possible set of grades. The variable midterm_grades is the Actual paramater. This means "what is actually used" for this call to the function, such as [90, 100, 70]; Nesting Functions?Often the question comes up: Can we Nest functions (can we place the code for one function inside another function)? The answer is a resounding: NO! It should be noted, that we can utilize (call) other functions inside a function, but we cannot create the recipe for a new function there. Note: Sadly this question often comes up when we are careless with our indentation and syntax, and accidentally forget to end our first function, thus, to the computer, placing our second function inside our first function: Back to Topics List What design technique can be used to break down an algorithm into function?Thus a design technique that programmers use to break down an algorithm into functions is known as top-down design ‾ \text{\underline{\textbf{top-down design}}} top-down design.
What is a design technique that helps to reduce the duplication of code within a program and is a benefit of using functions?within a program and is a benefit of using functions is code reuse.
How can functions make it easier for programs to be developed by teams of programmers?How can functions make the development of multiple programs faster? Functions can be written for the commonly needed tasks, and those functions can be incorporated into each program that needs them.
|