u nit 7 functions and modular programming introduction to c programming 1

17
UNIT 7 Functions and Modular Programming Introduction to C Programming 1

Upload: justin-patton

Post on 27-Mar-2015

244 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

UNIT 7

Functions and Modular Programming

Introduction to C Programming

1

Page 2: U NIT 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

Page 3: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

SCOPES

What is a file scope?

What is a block scope?

What is a local scope?

3

Page 4: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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

Page 5: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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);}

Page 6: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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

Page 7: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

POINTERS

Pointers allow us to directly access a variable at its memory location.

This allows us to “by-pass” scoping limitations.

7

Page 8: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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

Page 9: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

9

FIGURE 3.6

FUNCTION SQRT AS A “BLACK BOX”

Page 10: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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

Page 11: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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);

}

Page 12: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

12

FIGURE 3.20

FUNCTION WITH INPUT ARGUMENTS AND ONE RESULT

Page 13: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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, &amps);void calcVoltageCurrent(double W, int R, double *volts, *amps){

*volts = sqrt(W * R);

*amps = sqrt(W / R);

}

Page 14: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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, &amps);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

Page 15: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

15

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

15

Page 16: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

16

FIGURE 6.11

STRUCTURE CHART FOR COMMON FRACTION PROBLEM

Page 17: U NIT 7 Functions and Modular Programming Introduction to C Programming 1

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