UNIT 7
Functions and Modular Programming
Introduction to C Programming
1
UNIT 7 KEY CONCEPTS
1. Identifier scope2. Pointers3. Function prototypes and function bodies4. Input parameters5. Output parameters6. Multiple functions7. Types of testing
2
SCOPES
What is a file scope?
What is a block scope?
What is a local scope?
3
4
SCOPE OF NAMES Identifiers (variables and functions) defined in the
main function can be accessed anywhere within the program Unless the same name is used within a function or
block Identifiers (variables and functions) defined in a
function can only be accessed within that function Identifiers (variables and functions) defined in a
block can only be accessed within that block
5
//Demo 1#include<stdio.h>int main(void){
int x = 0;printf("This x equals %d\n", x);{
int x=10;x += 10;printf(" This x equals %d\n", x);
}printf(" The original x equals %d\n", x);return(0);}
POINTERS
A pointer is a variable that holds the address of another variable.
It is an advanced concept, however for our purposes we will use it with certain functions.
Two new operators & is the “Address of” operator * is the “Direct Reference” operator
(sometimes called the dereference operator) Examples
6
POINTERS
Pointers allow us to directly access a variable at its memory location.
This allows us to “by-pass” scoping limitations.
7
POINTERS
int i = 5; is a integer variable equal to 5
int *p_i; is a pointer, reads as “pointer to I”
p_i = &i; assigns the address of i to the pointer of i
*p_i Reads as “the direct reference to i” This value is five
8
9
FIGURE 3.6
FUNCTION SQRT AS A “BLACK BOX”
10
FUNCTION TYPES
10
Return Type Purpose # Input Parameters
# Output Parameters Return
Result
Defined Type To return a single value 0+ 1 Same as type
void To produce a printed output 0+ 0 None
void To compute multiple results 0+ 2+ Pointers
void To modify input arguments 1+ none Pointers
11
FUNCTIONS WITH INPUT PARAMETERS Data is passed into the function, but
cannot be modified by the function.
int main(void) { double calcFr(double L, double C) ; //prototype double C1, L1, resonantfreq; C1 = 47E-6; L1 = 100E-3 resonantfreq = calcFr (C1, L1); //invocationreturn 0;}
double calcFr(double L, double C) { // definition double Fr;
Fr = 1 / (2*PI*sqrt(L*C));return (Fr);
}
12
FIGURE 3.20
FUNCTION WITH INPUT ARGUMENTS AND ONE RESULT
13
FUNCTION WITH MULTIPLE RESULTS
13
• Use the address of operator (&) before the name of the variable.
• Example:void calcVoltageCurrent(double W, int R, double *volts, *amps);
calcVoltageCurrent(W, R, &volts, &s);void calcVoltageCurrent(double W, int R, double *volts, *amps){
*volts = sqrt(W * R);
*amps = sqrt(W / R);
}
FUNCTION WITH MULTIPLE RESULTSint main(void){
void calcVoltageCurrent(double W, int R, double *V, double *I);double power = .1, volts, amps; int load = 1000;
calcVoltageCurrent(power, load, &volts, &s);printf("V is %f and \n I is %f\n", volts, amps);
return 0;}
void calcVoltageCurrent(double W, int R, double *V, double *I){
*V = sqrt(W * R); *I = sqrt(W / R);} 14
15
separate(value, &sn, &whl, &fr);
15
16
FIGURE 6.11
STRUCTURE CHART FOR COMMON FRACTION PROBLEM
TOP-DOWN VS. BOTTOM-UP TESTING
17
Create function stubs to test the main program.
Add each function as it is complete and retest.
Separately test each function before integration.
Create a driver to test the function.
Unit testing – testing a function
Integration testing – testing the complete program after all functions have been added
Top-down testing Bottom-up testing
Top Related