Download presentation
Presentation is loading. Please wait.
Published byJustin Patton Modified over 10 years ago
1
U NIT 7 Functions and Modular Programming Introduction to C Programming 1
2
U NIT 7 KEY CONCEPTS 1. Identifier scope 2. Pointers 3. Function prototypes and function bodies 4. Input parameters 5. Output parameters 6. Multiple functions 7. Types of testing 2
3
S COPES What is a file scope? What is a block scope? What is a local scope? 3
4
4 S COPE OF N AMES 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
5 //Demo 1 #include 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);}
6
P OINTERS 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
7
P OINTERS Pointers allow us to directly access a variable at its memory location. This allows us to by-pass scoping limitations. 7
8
P OINTERS 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
9 F IGURE 3.6 F UNCTION SQRT AS A B LACK B OX
10
10 F UNCTION TYPES 10 Return Type Purpose # Input Parameters # Output Parameters Return Result Defined TypeTo return a single value0+1Same as type voidTo produce a printed output0+0None voidTo compute multiple results0+2+ Pointers voidTo modify input arguments1+none Pointers
11
11 F UNCTIONS WITH I NPUT P ARAMETERS 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); //invocation return 0;} double calcFr(double L, double C) { // definition double Fr; Fr = 1 / (2*PI*sqrt(L*C)); return (Fr); }
12
12 F IGURE 3.20 F UNCTION WITH I NPUT A RGUMENTS AND O NE R ESULT
13
13 F UNCTION WITH M ULTIPLE R ESULTS 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); }
14
F UNCTION WITH M ULTIPLE R ESULTS int 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
15 separate( value, &sn, &whl, &fr); 15
16
16 F IGURE 6.11 S TRUCTURE C HART FOR C OMMON F RACTION P ROBLEM
17
T OP -D OWN VS. B OTTOM -U P T ESTING 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 testingBottom-up testing
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.