Docstoc

Modularity2

Document Sample
Modularity2 Powered By Docstoc
					Advantages of Modularity
• How independent is this code segment (function)?
• If one aspect of a program is changed, how much will that affect a
  given module?
• Think of a module with respect to:
  –   Program Logic (i.e. the algorithm)
  –   Arguments or parameters to the module
  –   Internal variables, constants, data structures
  –   Flow of control


Advantages of Modularity
• A modular program is easier to write and debug. Functional
  components can be written and debugged separately
• A modular program is easier to maintain and change  reusability
• A modular program is easier to control and divide among different
  programmers


Disadvantages of Modularity
• For large projects, obtaining modularity in logical design may be
  time-consuming, tedious work
• The modular approach occasionally requires more CPU time and
  Memory allocation (space)
• Modularity in Real-time or On-line systems may be difficult to
  achieve
Achieving Modularity
• Break algorithm into Small, Independent logical blocks of
  operations or data
• Draw flowcharts to trace execution
• Clearly delineate input, output
• Fly high "20,000 feet"
• Fly lower "10,000 feet"
• Fly to the ground
• …then start coding


& Address of Operator
• Passes the address of the referenced variable
• Pass by Reference in Functions
• Examples:


  scanf("%d", &my_int)

  separate(value, &sn, &whl, &fr);


Pass by Value vs. Pass by Reference
• Pass by Value:
  – copies the value of an argument into the formal parameter of the function
     x = sqrt(value);
• Pass by Reference:
  – the address of an argument is copied into the parameter
  – inside the function, the address is used to access the actual argument used
    in the call
separate(value, &sn, &whl, &fr);
Meaning of the * Symbol (3 uses)
• * binary operator for multiplication
    x * y;


• variable declaration: char *signp
    – signp is a pointer to a variable of type char


• "follow the pointer": unary indirection operator
      *signp = '+';
       *fracp = magnitude - *wholep;


Examples
/* Example 1 Incorrect swap function */
void swap(int a, int b);                              x                     y
int main()                                            3                     7
{
      int x, y;

       x = 3;
       y = 7;
       printf("%d %d\n", x, y);
                                                              Pass by
       swap(x, y);
                                                              value
       printf("%d %d", x, y);
}

void swap(int a, int b)
{                                                         a             b
      int tmp;
                                                          3             7
       tmp = a;
       a = b;
                                               tmp
       b = tmp;                                                   a             b
}                                               3
                                                                  7             3


In the above, x & y are NOT affected by what happens inside the scope of
"swap", since the values of x and y are passed by value to DIFFERENT variables
a and b.
/* Example 2 Correct swap function */
void swap(int *a, int *b);                           x                       y
int main()                                           3 7                     73
{
      int x, y;
                                                    C342                    C344
       x = 3;
       y = 7;
       printf("%d %d\n", x, y);                               Pass by
       swap(&x, &y);                                         Reference
       printf("%d %d", x, y);
}

void swap(int *a, int *b)
{                                                        a                  b
      int tmp;
                                                     C342                 C344
       tmp = *a;
       *a = *b;
                                               tmp
       *b = tmp;
}                                               3


In the above, the addresses of x and y are sent as values to a and b. Then tmp
is assigned the value stored in the variable that a is pointing to (x), 3. Then x is
assigned the value of y, and y is a assigned the value of tmp; e.g. for *a = *b,
the statement should be read "follow the pointer to the variable b is pointing to,
and assign its value to the variable that a is pointing to."



/* Example 3, pointer re-assignment */
void swap(int *a, int *b);
                                                               x                     y
int main()
{                                                              3                    73
      int x, y;
                                                             C342                   C344
       x = 3;
       y = 7;
       printf("%d %d\n", x, y);
       swap(&x, &y);                                                    Pass by
       printf("%d %d", x, y);                                          Reference
}

void swap(int *a, int *b)
{
      int tmp;                                                     a                b
                                                                                   C344
       tmp = *a;                                               C342
       a = b;                                                  C344
       *b = tmp;                                         tmp
}
                                                         3
In the above, the statement a = b assigns the value of b (an address) to a. In
other words, it merely changes where a is pointing. The value in x is unaffected.



/* Example 4, type mis-match */
void swap(int *a, int *b);                                x                      y
int main()                                                3                         7
{
      int x, y;                                          C342                   C344
      x = 3;
      y = 7;
      printf("%d %d\n", x, y);                                       Pass by
      swap(x, y);                                                      ???
      printf("%d %d", x, y);
}

void swap(int *a, int *b)
{
                                                               a                b
      int tmp;                                                3 ??             7 ??
      tmp = *a;
      *a = *b;                                      tmp
      *b = tmp;
}                                                    3


The above would compile with warnings, but would cause an access violation
(and hence a core dump) at runtime. This is because x and y are integers, a and
b are pointers to int variables; so x & a, for example are of different types, and
y & b are of different types. Essentially what happens are the values 3 and 7
are sent by value to a and b, and then when I try to access those places in
memory (at memory addresses '3' and '7') I cause an access violation.


> gcc -o ptrtest ptrtest.c
ptrtest.c: In function `main':
ptrtest.c:11: warning: passing arg 1 of `swap' makes pointer from
integer without a cast
ptrtest.c:11: warning: passing arg 2 of `swap' makes pointer from
integer without a cast
> ptrtest
3 7
Segmentation fault (core dumped)
Scope
• The scope rules of a language are the rules that govern whether a
  piece of code knows about or has access to another piece of code
  or data.


• The scope of a name refers to the region of a program where a
  particular meaning of a name is "visible" or can be referenced


Scope
• Each function is a discrete block of code
• A function's code is private to that function and cannot be accessed by any
  statement in any other function…
• …only through a call to that function
• Code and data defined within one function cannot interact with the code or
  data defined in another function because the two functions have a different
  scope.


Scope
• Variables defined within a function are called local variables
• A local variable comes into existence when the function is entered
  and destroyed upon exiting the function
• Constant Macros (#define's) have a scope that begins at their
  definition, and continues to the end of the source file

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:9
posted:8/30/2012
language:English
pages:6