Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Structural Decomposition
An approach used to handle complexity in
solving problems based on the divide and
conquer strategy
A larger problem (more complex) is broken up
into smaller problems (less complex)
The solution to the problem (a module) is a
combination of solutions of the smaller problems
(sub-modules)
Each sub-module also has its own input(s) and
output(s)
Structural Decomposition
Program
Function1
Function2
Function3
Functions
Modules in C
Programs combine user-defined functions with library functions
C standard library has a wide variety of functions
Function calls
Invoking functions
Provide function name and arguments (data)
Function performs operations or manipulations
Function returns results
main
worker1
worker4
worker2
worker5
worker3
Description
Example
sqrt( x )
square root of x
exp( x )
exponential function ex
log( x )
natural logarithm of x
(base e)
exp(
exp(
log(
log(
log10( x )
fabs( x )
absolute value of x
ceil( x )
floor( x )
pow( x, y )
fmod( x, y )
remainder of x/y as a
floating point number
pow( 2, 7 ) is 128.0
pow( 9, .5 ) is 3.0
fmod( 13.657, 2.333 ) is
1.992
sin( x )
trigonometric sine of x
(x in radians)
cos( x )
trigonometric cosine of x
(x in radians)
tan( x )
1.0 ) is
2.0 ) is
2.718282
7.389056
2.718282
7.389056
) is 1.0
) is 2.0
Functions
Functions
Modularize a program
All variables defined inside functions are local variables
Known only in function defined
Parameters
Communicate information between functions
Local variables
Benefits of functions
Divide and conquer
Manageable program development
Software reusability
Use existing functions as building blocks for new programs
Abstraction - hide internal details (library functions)
User-Defined Functions
Functions designed, written or defined by programmers
Every user-defined function must have 3 elements
a) function definitions
b) function prototypes
c) function calls
A function must be called in order to execute the
statements contained in its definition
a) Function Definitions
Function definition format
return-value-type
data type of the result (default int)
void indicates that the function returns nothing
function-name
any valid identifier
parameter-list
comma separated list, declares parameters received by the
function when it is called
a type must be listed explicitly for each parameter
if function does not receive any values, parameter-list is void
returning control
If nothing returned :
return OR
until reaches right brace
If something returned
return expression;
b) Function Prototypes
Function-name
Parameters > what the function takes in
Return value type > data type function returns (default int)
Give the compiler the type of data returned by the function,
the number of parameters the function expects to receive,
the types of the parameters, and the order in which these
parameters are expected
Use to validate functions
Prototype only needed if function definition comes after use
in program
Example : The function with the prototype
c) Function Calls
To execute a function, it must first be called with the
following information :
Stating the function name
Providing a certain information (parameter)
2
3
#include <stdio.h>
4
5
/* function prototype */
6
7
int main()
{
int x; /* counter */
10
11
12
13
14
} /* end for */
15
16
printf( "\n" );
17
18
19
20
21 } /* end main */
22
23 /* square function definition returns square of an integer */
26
27
16
25
36
49
64
81
100
/* Fig. 5.4:
Finding the maximum of three integers */
2
3
#include <stdio.h>
4
5
6
7
int main()
10
11
12
13
14
15
16
17
18
19
20
21
22
23 } /* end main */
24
int max = x;
/* assume x is largest */
30
31
32
max = y;
33
} /* end if */
34
35
36
max = z;
37
} /* end if */
38
39
return max;
40
41 } /* end function maximum */
Enter three
Maximum is:
Enter three
Maximum is:
Enter three
Maximum is:
integers: 22 85 17
85
integers: 85 22 17
85
integers: 22 17 85
85
E
X
A
M
P
L
E
#include <stdio.h>
int computeArea(int width, int height);
void outputArea(int area);
int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeArea(rectWidth, rectHeight);
outputArea(rectArea);
return 0;
}
int computeArea(int width, int height) {
return width * height;
}
void outputArea(int area) {
printf(Rectangle area: %d\n", area);
}
#include <stdio.h>
int computeArea(int width, int height);
void outputArea(int area);
int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeArea(rectWidth, rectHeight);
outputArea(rectArea);
return 0;
}
int computeArea(int width, int height) {
return width * height;
}
_
void outputArea(int area) {
printf(Rectangle area: %d\n", area);
}
???
rectWidth
???
rectHeight
???
rectArea
#include <stdio.h>
int computeArea(int width, int height);
void outputArea(int area);
int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeArea(rectWidth, rectHeight);
outputArea(rectArea);
return 0;
}
int computeArea(int width, int height) {
return width * height;
}
7 11
void outputArea(int area) {
_
printf(Rectangle area: %d\n",
area);
}
7
rectWidth
11
rectHeight
???
rectArea
#include <stdio.h>
int computeArea(int width, int height);
void outputArea(int area);
int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
77
rectArea = computeArea(rectWidth, rectHeight);
outputArea(rectArea);
return 0;
}
7
int computeArea(int width, int height) {
width
return width * height;
}
7 11
void outputArea(int area) {
_
printf(Rectangle area: %d\n",
area);
}
7
rectWidth
11
rectHeight
???
rectArea
11
height
#include <stdio.h>
int computeArea(int width, int height);
void outputArea(int area);
int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeArea(rectWidth, rectHeight);
outputArea(rectArea);
return 0;
}
int computeArea(int width, int height) {
return width * height;
}
7 11
void outputArea(int area) {
printf(Rectangle area: _%d\n", area);
}
7
rectWidth
11
rectHeight
77
rectArea
#include <stdio.h>
int computeArea(int width, int height);
void outputArea(int area);
int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeArea(rectWidth, rectHeight);
outputArea(rectArea);
}
11
77 11
int computeArea(int width, intRectangle
height) { area: 77
_
return width * height;_
}
void outputArea(int area) {
printf(Rectangle area: %d\n", area);
}
77
area
7
rectWidth
#include <stdio.h>
int computeArea(int width, int height);
11
void outputArea(int area);
rectHeight
int main() {
int rectWidth, rectHeight, rectArea;
77
scanf("%d%d", &rectWidth, &rectHeight);
rectArea
rectArea = computeArea(rectWidth, rectHeight);
outputArea(rectArea);
}
7 11
int computeArea(int width, int height) {
Rectangle area: 77
return width * height;
_
}
void outputArea(int area) {
printf(Rectangle area: %d\n", area);
}
Global Variables
Global variables can be accessed or known by
any function comprising the program
Global variables must be declared outside of any function
even outside of the main function
Local Variables
int computeRectArea(int, int);
int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeRectArea(rectWidth, rectHeight);
printf(Rectangle area: %d", area);
}
int computeRectArea(int width, int height) {
int area;
area = width * height;
return area;
}
Local Variables
int computeRectArea(int, int);
int main() {
The local variable area is only
int rectWidth, rectHeight,used
rectArea;
within the function The function which defines it
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeRectArea(rectWidth, rectHeight);
printf(Rectangle area: %d", area);
}
int computeRectArea(int width, int height) {
int area;
area = width * height;
return area;
}
Local Variables
int computeRectArea(int, int);
Variable area in function
int main() {
computeRectArea()
int rectWidth, rectHeight;
cannot be accessed here.
int rectArea;
scanf("%d%d", &rectWidth, &rectHeight);
rectArea = computeRectArea(rectWidth, rectHeight);
printf(Rectangle area: %d", area);
ERROR!
}
int computeRectArea(int width, int height) {
int area;
area = width * height;
return area;
}
Recursive Function
Is a function that can call itself and when a
function calls itself, a new copy of that
function is run.
The recursive function must have a stop
condition
Body of function must have 3 components
Recursive part contains a call to itself
Termination part a value to stop the recursive
function
A decision construct that will eventually select
the termination part
5!
5!
5 * 4!
4 * 3!
3 * 2!
2 * 1!
1
( a ) Se q u en c e o f re c ursive c a lls.
5! = 5 * 24 = 120 is re turn ed
5 * 4!
4! = 4 * 6 = 24 is re turne d
4 * 3!
3! = 3 * 2 = 6 is re tu rn e d
3 * 2!
2! = 2 * 1 = 2 is re turne d
2 * 1!
1 re turne d
1
( b ) Va lue s re turne d fro m e a c h re cu rsive c a ll.
End of Lecture