fundamentals of programming c session 11
play

Fundamentals of Programming C - PowerPoint PPT Presentation

Fundamentals of Programming C Session # 11 By: Saeed Haratian Fall 2015 Outlines Structured Programming Summary Program Modules in C Math Library Functions Function


  1. بميـــحرلا نحنحنرلا للوللوا مــس Fundamentals of Programming C Session # 11 By: Saeed Haratian Fall 2015

  2. Outlines  Structured Programming Summary  Program Modules in C  Math Library Functions  Function Definitions  Function Prototypes

  3. Structured Programming Summary  Figure 4.17 summarizes the control statements discussed previously.  Small circles are used in the figure to indicate the single entry point and the single exit point of each statement.  Connecting individual flowchart symbols arbitrarily can lead to unstructured programs.  Therefore, the programming profession has chosen to combine flowchart symbols to form a limited set of control statements, and to build only structured programs by properly combining control statements in two simple ways.  For simplicity, only single-entry/single-exit control statements are used — there is only one way to enter and only one way to exit each control statement.

  4. Structured Programming Summary …

  5. Structured Programming Summary …

  6. Structured Programming Summary …  Connecting control statements in sequence to form structured programs is simple — the exit point of one control statement is connected directly to the entry point of the next, i.e., the control statements are simply placed one after another in a program — we have called this “ control-statement stacking .”  The rules for forming structured programs also allow for control statements to be nested.

  7. Structured Programming Summary …  Figure 4.18 shows the rules for forming structured programs.  The rules assume that the rectangle flowchart symbol may be used to indicate any action including input/output.

  8. Structured Programming Summary …  Figure 4.19 shows the simplest flowchart.

  9. Structured Programming Summary …  Applying the rules of Fig. 4.18 always results in a structured flowchart with a neat, building-block appearance.  Repeatedly applying Rule 2 to the simplest flowchart (Fig. 4.19) results in a structured flowchart containing many rectangles in sequence (Fig. 4.20).  Notice that Rule 2 generates a stack of control statements; so we call Rule 2 the stacking rule.

  10. Structured Programming Summary …

  11. Structured Programming Summary …  Rule 4 generates larger, more involved, and more deeply nested structures.  The flowcharts that emerge from applying the rules in Fig. 4.18 constitute the set of all possible structured flowcharts and hence the set of all possible structured programs.  It’s because of the elimination of the goto statement that these building blocks never overlap one another.  The beauty of the structured approach is that we use only a small number of simple single-entry/single-exit pieces, and we assemble them in only two simple ways.  Figure 4.22 shows the kinds of stacked building blocks that emerge from applying Rule 2 and the kinds of nested building blocks that emerge from applying Rule 3.

  12. Structured Programming Summary …

  13. Structured Programming Summary …  The figure also shows the kind of overlapped building blocks that cannot appear in structured flowcharts (because of the elimination of the goto statement).  If the rules in Fig. 4.18 are followed, an unstructured flowchart (such as that in Fig. 4.23) cannot be created.  If you’re uncertain whether a particular flowchart is structured, apply the rules of Fig. 4.18 in reverse to try to reduce the flowchart to the simplest flowchart.  If you succeed, the original flowchart is structured; otherwise, it’s not.  Structured programming promotes simplicity.

  14. Structured Programming Summary …

  15. Structured Programming Summary …

  16. Structured Programming Summary …  Bohm and Jacopini showed that only three forms of control are needed:  Sequence  Selection  Repetition  Sequence is straighforward.  In fact, it’s straightforward to prove that the simple if statement is sufficient to provide any form of selection.  It’s straightforward to prove that the while statement is sufficient to provide any form of repetition.

  17. Structured Programming Summary …  Combining these results illustrates that any form of control ever needed in a C program can be expressed in terms of only three forms of control:  sequence  if statement (selection)  while statement (repetition)  And these control statements can be combined in only two ways — stacking and nesting.  Indeed, structured programming promotes simplicity.

  18. Program Modules in C  Most computer programs that solve real-world problems are much larger than the programs presented in the first few chapters.  Experience has shown that the best way to develop and maintain a large program is to construct it from smaller pieces or modules, each of which is more manageable than the original program.  This technique is called divide and conquer.  This chapter describes the features of the C language that facilitate the design, implementation, operation and maintenance of large programs.  The functions printf , scanf and pow that we’ve used in previous chapters are Standard Library functions.

  19. Program Modules in C …  You can write your own functions to define tasks that may be used at many points in a program.  These are sometimes referred to as programmer-defined functions.  The statements defining the function are written only once, and the statements are hidden from other functions.  Functions are invoked by a function call, which specifies the function name and provides information (as arguments) that the called function needs to perform its designated task.  A common analogy for this is the hierarchical form of management.  A boss (the calling function or caller) asks a worker (the called function) to perform a task and report back when the task is done.

  20. Program Modules in C …  For example, a function needing to display information on the screen calls the worker function printf to perform that task, then printf displays the information and reports back — or returns — to the calling function when its task is completed.  The boss function does not know how the worker function performs its designated tasks.  The worker may call other worker functions, and the boss will be unaware of this.  We’ll soon see how this “hiding” of implementation details promotes good software engineering.  Note that worker1 acts as a boss function to worker4 and worker5.  Relationships among functions may differ from the hierarchical structure shown in this figure.

  21. Program Modules in C …

  22. Math Library Functions  Math library functions allow you to perform certain common mathematical calculations.  Functions are normally used in a program by writing the name of the function followed by a left parenthesis followed by the argument (or a comma-separated list of arguments) of the function followed by a right parenthesis.  For example, a programmer desiring to calculate and print the square root of 900.0 might write printf( "%. "%.2f" f", sqrt( , sqrt( 900.0 900.0 ) ); ) );

  23. Math Library Functions …  When this statement executes, the math library function sqrt is called to calculate the square root of the number contained in the parentheses (900.0).  The number 900.0 is the argument of the sqrt function.  The preceding statement would print 30.00.  The sqrt function takes an argument of type double and returns a result of type double .  All functions in the math library that return floating point values return the data type double .  Note that double values, like float values, can be output using the %f conversion specification.

  24. Math Library Functions …

  25. Math Library Functions …

  26. Functions  Functions allow you to modularize a program.  All variables defined in function definitions are local variables — they’re known only in the function in which they’re defined.  Most functions have a list of parameters that provide the means for communicating information between functions.  A function’s parameters are also local variables of that function.  With good function naming and definition, programs can be created from standardized functions that accomplish specific tasks, rather than being built by using customized code.  This is known as abstraction.  We use abstraction each time we use standard library functions like printf , scanf and pow .

  27. Functions  There are several motivations for “functionalizing” a program.  The divide-and-conquer approach makes program development more manageable.  Another motivation is software reusability — using existing functions as building-blocks to create new programs.  Software reusability is a major factor in the object-oriented programming movement that you’ll learn more about when you study languages derived from C, such as C++, Java and C# (pronounced “C sharp”).  A third motivation is to avoid repeating code in a program.  Packaging code as a function allows the code to be executed from several locations in a program simply by calling the function.

  28. Functions …

Recommend


More recommend